پودمان:AlborzEducation
توضیحات این پودمان میتواند در پودمان:AlborzEducation/توضیحات قرار گیرد.
--------------------------------------------------------------------------------
-- Module:AlborzEducation
-- Wiki Alborz Education Infobox Engine
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
-- ARGUMENTS
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
-- NORMALIZE PARAMETER NAMES
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
-- FIELD VALUE
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
-- 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 NORMALIZER
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
-- SOCIAL ID EXTRACTOR
--------------------------------------------------------------------------------
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 FORMATTER
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
-- FIELD RENDERING
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD RENDERING
--------------------------------------------------------------------------------
local function renderField(fieldName, value)
local field = fields[fieldName]
if not field or not value then
return nil
end
------------------------------------------------------------------------
-- IMAGE
------------------------------------------------------------------------
if field.type == "image" then
local width = "250px"
if fieldName == "logo" then
width = "120px"
end
return mw.html.create("tr")
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-image")
:css("text-align", "center")
:wikitext(imageMarkup(value, width))
:done()
end
------------------------------------------------------------------------
-- BOOLEAN
------------------------------------------------------------------------
if field.type == "boolean" then
value = formatBoolean(value)
end
------------------------------------------------------------------------
-- WEBSITE
------------------------------------------------------------------------
if fieldName == "website" then
local url = normalizeUrl(value)
value = string.format(
"[%s وبگاه رسمی]",
url
)
------------------------------------------------------------------------
-- EMAIL
------------------------------------------------------------------------
elseif field.type == "email" then
value = string.format(
"[mailto:%s %s]",
value,
value
)
------------------------------------------------------------------------
-- INSTAGRAM
------------------------------------------------------------------------
elseif fieldName == "instagram" then
if not value:match("^{{") then
local id = extractId(value)
value = string.format(
"[https://instagram.com/%s @%s]",
id,
id
)
end
------------------------------------------------------------------------
-- TELEGRAM
------------------------------------------------------------------------
elseif fieldName == "telegram" then
if not value:match("^{{") then
local id = extractId(value)
value = string.format(
"[https://t.me/%s @%s]",
id,
id
)
end
------------------------------------------------------------------------
-- APARAT
------------------------------------------------------------------------
elseif fieldName == "aparat" then
if not value:match("^{{") then
local id = extractId(value)
value = string.format(
"[https://www.aparat.com/%s صفحهٔ آپارات]",
id
)
end
------------------------------------------------------------------------
-- WIKIPEDIA
------------------------------------------------------------------------
elseif fieldName == "wikipedia" then
if not value:match("^%[%[") then
value = string.format("[[%s]]", value)
end
------------------------------------------------------------------------
-- OTHER URL FIELDS
------------------------------------------------------------------------
elseif field.type == "url" then
local url = normalizeUrl(value)
value = string.format(
"[%s %s]",
url,
value
)
end
------------------------------------------------------------------------
-- NORMAL ROW
------------------------------------------------------------------------
local row = mw.html.create("tr")
row:tag("th")
:wikitext(field.label)
row:tag("td")
:wikitext(value)
return row
end
--------------------------------------------------------------------------------
-- TITLE
--------------------------------------------------------------------------------
local function renderTitle(box, args)
local logo = getFieldValue(args, "logo")
local title = getFieldValue(args, "fullname")
or getFieldValue(args, "name")
------------------------------------------------------------------------
-- LOGO
------------------------------------------------------------------------
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
------------------------------------------------------------------------
-- TITLE
------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
local function renderImage(box, args)
local image = getFieldValue(args, "image")
if not image then
return
end
local caption = getFieldValue(args, "caption")
box:tag("tr")
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-image")
:css("text-align", "center")
:wikitext(imageMarkup(image, "250px"))
------------------------------------------------------------------------
-- IMAGE 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)
local hasData = false
for _, field in ipairs(fieldList) do
if getFieldValue(args, field) then
hasData = true
break
end
end
if not hasData then
return
end
local group = groups[groupName]
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)
for _, field in ipairs(fieldList) do
local value = getFieldValue(args, field)
if value then
local row = renderField(field, value)
if row then
box:node(row)
end
end
end
end
--------------------------------------------------------------------------------
-- LAYOUT
--------------------------------------------------------------------------------
local function renderLayout(layoutName, args)
local layout = layouts[layoutName]
if not layout then
layout = layouts.school
end
local box = mw.html.create("table")
:addClass(INFOBOX_CLASS)
renderTitle(box, args)
renderImage(box, args)
for _, section in ipairs(layout) do
renderGroup(
box,
section.group,
section.fields,
args
)
end
return tostring(box)
end
--------------------------------------------------------------------------------
-- MAIN
--------------------------------------------------------------------------------
function p.main(frame)
local args = normalizeArgs(
getArgs(frame)
)
------------------------------------------------------------------------
-- انتخاب نوع چیدمان
------------------------------------------------------------------------
local layout = "school"
local t = (args.type or ""):lower()
if t == "دانشگاه" or t == "university" then
layout = "university"
elseif t == "حوزه علمیه" or t == "seminary" then
layout = "seminary"
elseif t == "آموزشگاه" or t == "training_center" then
layout = "training_center"
elseif t == "پژوهشکده" or t == "research_center" then
layout = "research_center"
elseif t == "کالج" or t == "college" then
layout = "college"
elseif t == "مهدکودک" or t == "kindergarten" then
layout = "kindergarten"
end
return renderLayout(layout, args)
end
--------------------------------------------------------------------------------
-- RETURN
--------------------------------------------------------------------------------
return p