پرش به محتوا

پودمان:AlborzEducation: تفاوت میان نسخه‌ها

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
 
(۱۲ نسخهٔ میانیِ ایجادشده توسط همین کاربر نشان داده نشد)
خط ۲: خط ۲:
-- Module:AlborzEducation
-- Module:AlborzEducation
-- Wiki Alborz Education Infobox Engine
-- Wiki Alborz Education Infobox Engine
--
-- نسخهٔ نهایی با تشخیص هوشمند و مقاوم layout برای آموزشگاه هنری
-- Rendering engine for education information boxes.
-- Configuration is stored in Module:AlborzEducation/Data
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local p = {}
local p = {}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- CSS CLASSES
-- CONFIGURATION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local INFOBOX_CLASS = "infobox education-infobox"
local INFOBOX_CLASS = "infobox education-infobox"
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- LOAD DATA
-- LOAD DATA
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local data = mw.loadData("Module:AlborzEducation/Data")
local data = mw.loadData(
    "Module:AlborzEducation/Data"
)
 
 
 
--------------------------------------------------------------------------------
-- LOCAL REFERENCES
--------------------------------------------------------------------------------
 
local lookup = data.lookup
local lookup = data.lookup
local fields = data.fieldDefinitions
local fields = data.fieldDefinitions
local layouts = data.layouts
local layouts = data.layouts
local groups = data.groups
local groups = data.groups
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- BASIC UTILITIES
-- BASIC UTILITIES
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function trim(value)
local function trim(value)
 
     if value == nil then
     if not value then
         return nil
         return nil
     end
     end
 
     value = mw.text.trim(tostring(value))
 
    value = tostring(value)
 
     value = mw.text.trim(value)
 
 
     if value == "" then
     if value == "" then
         return nil
         return nil
     end
     end
     return value
     return value
end
end
--------------------------------------------------------------------------------
-- ARGUMENT HANDLING
--------------------------------------------------------------------------------


local function getArgs(frame)
local function getArgs(frame)
     local args = {}
     local args = {}
 
     for k, v in pairs(frame.args) do
 
         k = trim(k)
     for key, value in pairs(frame.args) do
         v = trim(v)
 
         if k and v then
         key = trim(key)
             args[k] = v
         value = trim(value)
 
 
         if key and value then
             args[key] = value
         end
         end
     end
     end
     return args
     return args
end
end
--------------------------------------------------------------------------------
-- NORMALIZE PARAMETERS
--------------------------------------------------------------------------------


local function normalizeArgs(args)
local function normalizeArgs(args)
     local result = {}
     local result = {}
     for key, value in pairs(args) do
     for key, value in pairs(args) do
 
         local newKey = lookup[key] or key
 
         result[newKey] = value
         local internalName = lookup[key]
 
 
        if internalName then
 
            result[internalName] = value
 
 
         else
 
            -- allow direct internal field names
            result[key] = value
 
        end
 
     end
     end
     return result
     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
end


 
local function isAlreadyFormatted(value)
 
    return value:match("^{{") or value:match("^%[") or value:match("^%[%[")
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD VALUE GETTER
-- IMAGE HELPER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function imageMarkup(file, width)
local function getFieldValue(args, fieldName)
     if not file then
 
         return nil
 
    local value = args[fieldName]
 
 
    if value then
        return value
    end
 
 
 
    local definition = fields[fieldName]
 
 
     if definition and definition.default then
         return definition.default
     end
     end
 
    width = width or "250px"
 
     return string.format("[[پرونده:%s|%s|center]]", file, width)
 
     return nil
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD RENDERING
-- URL & ID HELPERS
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function normalizeUrl(url)
local function renderField(fieldName, value)
     url = trim(url)
 
     if not url then
 
     local field = fields[fieldName]
 
 
     if not field then
         return nil
         return nil
     end
     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
     if not value then
         return nil
         return nil
     end
     end
 
    value = value:gsub("^https?://", "")
 
    value = value:gsub("^www%.", "")
 
    value = value:gsub("^instagram%.com/", "")
     local label = field.label
     value = value:gsub("^t%.me/", "")
     local fieldType = field.type
     value = value:gsub("^aparat%.com/", "")
 
    value = value:gsub("^@", "")
 
    value = value:gsub("/$", "")
 
    return value
    ----------------------------------------------------------------------------
end
    -- Image fields
--------------------------------------------------------------------------------
    ----------------------------------------------------------------------------
-- BOOLEAN
 
--------------------------------------------------------------------------------
     if fieldType == "image" then
local function formatBoolean(value)
 
     local yes = {
 
        ["بله"] = true, ["بلی"] = true,
         local row = mw.html.create("tr")
         ["yes"] = true, ["Yes"] = true, ["YES"] = true,
 
         ["true"] = true, ["True"] = true, ["TRUE"] = true,
 
        ["1"] = true
         row
    }
            :tag("td")
    if yes[tostring(value)] then
            :attr("colspan", "2")
        return "دارد"
            :addClass("education-infobox-image")
    end
            :wikitext(
    return "ندارد"
                string.format(
end
                    "[[پرونده:%s|250px|center]]",
--------------------------------------------------------------------------------
                    value
-- LIST HELPER
                )
--------------------------------------------------------------------------------
            )
local function formatList(value)
            :done()
    if isAlreadyFormatted(value) then
 
         return value
 
         return row
 
     end
     end
 
     value = value:gsub("،", ",")
 
    local items = {}
 
    for item in mw.text.gsplit(value, ",") do
     ----------------------------------------------------------------------------
        item = trim(item)
    -- Boolean fields
         if item and item ~= "" then
    ----------------------------------------------------------------------------
             table.insert(items, "* " .. item)
 
    if fieldType == "boolean" then
 
 
        if value == "yes"
            or value == "بله"
            or value == "true"
            or value == "1"
         then
 
            value = "دارد"
 
        else
 
             value = "ندارد"
 
         end
         end
     end
     end
 
     if #items == 0 then
 
         return value
 
    ----------------------------------------------------------------------------
    -- URL fields
    ----------------------------------------------------------------------------
 
     if fieldType == "url" then
 
 
         value = string.format(
            "[%s %s]",
            value,
            value
        )
 
 
     end
     end
 
     return table.concat(items, "\n")
 
 
     ----------------------------------------------------------------------------
    -- Normal text fields
    ----------------------------------------------------------------------------
 
    local row = mw.html.create("tr")
 
 
    row
        :tag("th")
        :wikitext(label)
        :done()
 
 
 
    row
        :tag("td")
        :wikitext(value)
        :done()
 
 
 
    return row
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- INFOBOX TITLE
-- 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 renderTitle(args)
local function formatValue(fieldName, value, fieldDef)
 
     if not value then
 
    local title = getFieldValue(
        args,
        "name"
    )
 
 
     if not title then
         return nil
         return nil
     end
     end
 
     local formatterName = (fieldDef and fieldDef.formatter) or fieldName
 
    if fieldDef and fieldDef.type and not formatters[formatterName] then
 
        if fieldDef.type == "boolean" then
     local row = mw.html.create("tr")
            formatterName = "boolean"
 
        elseif fieldDef.type == "email" then
 
            formatterName = "email"
    row
         elseif fieldDef.type == "url" then
         :tag("th")
            formatterName = "url"
        :attr("colspan", "2")
        elseif fieldDef.type == "list" then
        :addClass("education-infobox-title")
            formatterName = "list"
         :wikitext(title)
         end
         :done()
    end
 
    local formatter = formatters[formatterName]
 
    if formatter then
 
         return formatter(value)
     return row
    end
 
     return value
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- IMAGE RENDERING
-- FIELD RENDERING
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function renderField(fieldName, value)
local function renderImage(args)
     local field = fields[fieldName]
 
     if not field or not value then
 
     local image = getFieldValue(
        args,
        "image"
    )
 
 
     if not image then
         return nil
         return nil
     end
     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")
     local row = mw.html.create("tr")
 
     row:tag("th"):wikitext(field.label or fieldName)
 
    row:tag("td"):wikitext(value)
     row
        :tag("td")
        :attr("colspan", "2")
        :addClass("education-infobox-image")
        :wikitext(
            string.format(
                "[[پرونده:%s|250px|center]]",
                image
            )
        )
        :done()
 
 
 
     return row
     return row
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GROUP RENDERING
-- 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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GROUP RENDERING
-- MAIN IMAGE + CAPTION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function renderImage(box, args)
local function renderGroup(box, groupName, fieldList, args)
     local image = getFieldValue(args, "image")
 
     if not image then
     local group = groups[groupName]
 
     if not group then
         return
         return
     end
     end
 
    box:tag("tr")
     --------------------------------------------------------------------------
        :tag("td")
    -- Check whether the group has at least one visible field
            :attr("colspan", "2")
    --------------------------------------------------------------------------
            :addClass("education-infobox-image")
 
            :css("text-align", "center")
     local hasValue = false
            :wikitext(imageMarkup(image, "250px"))
 
    local caption = getFieldValue(args, "caption")
     for _, fieldName in ipairs(fieldList) do
    if caption then
         if getFieldValue(args, fieldName) then
        box:tag("tr")
             hasValue = true
            :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
             break
         end
         end
     end
     end
 
     if not hasData then
     if not hasValue then
         return
         return
     end
     end
 
     local group = groups[groupName]
     --------------------------------------------------------------------------
     if group then
     -- Group header
        box:tag("tr")
    --------------------------------------------------------------------------
            :tag("th")
 
                :attr("colspan", "2")
    local header = box:tag("tr")
                :addClass("education-infobox-group")
 
                :css("text-align", "center")
    header
                :css("font-weight", "bold")
        :tag("th")
                :css("padding", "6px")
        :attr("colspan", "2")
                :wikitext((group.icon or "") .. " " .. (group.label or groupName))
        :addClass("education-infobox-group")
     end
        :wikitext((group.icon or "") .. " " .. group.label)
     for _, field in ipairs(fieldList) do
 
        if not rendered[field] then
    --------------------------------------------------------------------------
            local value = getFieldValue(args, field)
    -- Fields
            if value then
     --------------------------------------------------------------------------
                local row = renderField(field, value)
 
                if row then
     for _, fieldName in ipairs(fieldList) do
                    box:node(row)
 
                    rendered[field] = true
        local value = getFieldValue(args, fieldName)
                end
 
        if value then
 
            local row = renderField(fieldName, value)
 
            if row then
                box:node(row)
             end
             end
         end
         end
     end
     end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- LAYOUT RENDERING
-- تشخیص هوشمند layout (نسخهٔ قوی و مقاوم)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function detectLayout(typeValue)
    if not typeValue or typeValue == "" then
        return "school"
    end


local function renderLayout(layoutName, args)
    -- نرمال‌سازی قوی
    local t = mw.ustring.lower(typeValue)
    t = t:gsub("‌", "")          -- حذف نیم‌فاصله
    t = t:gsub("%s+", "")        -- حذف همه فاصله‌ها
    t = t:gsub("ي", "ی")          -- یکسان‌سازی ی
    t = t:gsub("ك", "ک")         -- یکسان‌سازی ک


    -- مطابقت دقیق بعد از نرمال‌سازی
    local normalizedMap = {
        ["دانشگاه"]        = "university",
        ["university"]      = "university",
        ["حوزهعلمیه"]      = "seminary",
        ["seminary"]        = "seminary",
        ["آموزشگاه"]        = "training_center",
        ["training_center"] = "training_center",
        ["آموزشگاههنری"]    = "art_school",
        ["آموزشگاههنر"]    = "art_school",
        ["هنری"]            = "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",
    }


     local layout = layouts[layoutName]
     if normalizedMap[t] then
        return normalizedMap[t]
    end


 
    -- بررسی کلمات کلیدی
     if not layout then
    if t:find("هنری") or t:find("هنر") or t:find("آکادمی") then
         return nil
        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
     end


 
     return "school"
 
     local box = mw.html.create("table")
        :addClass(INFOBOX_CLASS)
 
 
 
    ----------------------------------------------------------------------------
    -- Header
    ----------------------------------------------------------------------------
 
local title = renderTitle(args)
 
if title then
 
    box
        :node(title)
 
end
end
 
--------------------------------------------------------------------------------
 
-- RESOLVE INHERIT
 
--------------------------------------------------------------------------------
local image = renderImage(args)
local function resolveLayout(layoutName)
 
    local seen = {}
if image then
    local current = layoutName
 
    while current do
    box
        if seen[current] then
        :node(image)
            return layouts.school or {}
 
        end
end
        seen[current] = true
 
         local layout = layouts[current]
 
         if not layout then
 
            return layouts.school or {}
    ----------------------------------------------------------------------------
        end
    -- Content Groups
         if layout.inherit then
    ----------------------------------------------------------------------------
            current = layout.inherit
for _, section in ipairs(layout) do
        else
 
            return layout
    renderGroup(
         end
         box,
     end
         section.group,
    return layouts.school or {}
         section.fields,
         args
     )
 
end
end
    return box
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- MAIN FUNCTION
-- RENDER LAYOUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function renderLayout(layoutName, args)
function p.main(frame)
     local layout = resolveLayout(layoutName)
 
     local box = mw.html.create("table")
 
         :addClass(INFOBOX_CLASS)
     local rawArgs = getArgs(frame)
        :addClass("education-" .. layoutName)
 
     local rendered = {}
 
    renderTitle(box, args)
 
    rendered.logo = true
     local args = normalizeArgs(
     renderImage(box, args)
         rawArgs
     rendered.image = true
    )
    rendered.caption = true
 
     for _, section in ipairs(layout) do
 
         renderGroup(box, section.group, section.fields, args, rendered)
 
     local layoutName = "school"
 
 
 
     local result = renderLayout(
        layoutName,
        args
     )
 
 
 
     if result then
 
         return tostring(result)
 
     end
     end
 
     return tostring(box)
 
 
     return ""
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- RETURN MODULE
-- MAIN
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function p.main(frame)
    local args = normalizeArgs(getArgs(frame))
    local layout = detectLayout(args.type)
    return renderLayout(layout, args)
end


return p
return p

نسخهٔ کنونی تا ‏۲۵ اوت ۲۰۲۶، ساعت ۲۱:۵۴

توضیحات این پودمان می‌تواند در پودمان:AlborzEducation/توضیحات قرار گیرد.

--------------------------------------------------------------------------------
-- Module:AlborzEducation
-- Wiki Alborz Education Infobox Engine
-- نسخهٔ نهایی با تشخیص هوشمند و مقاوم 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
    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 (نسخهٔ قوی و مقاوم)
--------------------------------------------------------------------------------
local function detectLayout(typeValue)
    if not typeValue or typeValue == "" then
        return "school"
    end

    -- نرمال‌سازی قوی
    local t = mw.ustring.lower(typeValue)
    t = t:gsub("‌", "")          -- حذف نیم‌فاصله
    t = t:gsub("%s+", "")         -- حذف همه فاصله‌ها
    t = t:gsub("ي", "ی")          -- یکسان‌سازی ی
    t = t:gsub("ك", "ک")          -- یکسان‌سازی ک

    -- مطابقت دقیق بعد از نرمال‌سازی
    local normalizedMap = {
        ["دانشگاه"]         = "university",
        ["university"]      = "university",
        ["حوزهعلمیه"]       = "seminary",
        ["seminary"]        = "seminary",
        ["آموزشگاه"]        = "training_center",
        ["training_center"] = "training_center",
        ["آموزشگاههنری"]    = "art_school",
        ["آموزشگاههنر"]     = "art_school",
        ["هنری"]            = "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",
    }

    if normalizedMap[t] then
        return normalizedMap[t]
    end

    -- بررسی کلمات کلیدی
    if 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