پرش به محتوا

پودمان:InfoboxPerson/Core: تفاوت میان نسخه‌ها

از ویکی البرز
صفحه‌ای تازه حاوی «local p = {} --================================================== -- Module:InfoboxPerson/Core -- Version 2 -- Part 1 --================================================== local html = mw.html ---------------------------------------------------- -- LOAD DATA ---------------------------------------------------- local data = require("Module:InfoboxPerson/Data") local lookup = data.lookup or {} local layouts = data.layouts or {} loc...» ایجاد کرد
 
جزبدون خلاصۀ ویرایش
 
(۵ نسخهٔ میانیِ ایجادشده توسط همین کاربر نشان داده نشد)
خط ۱: خط ۱:
local p = {}
local p = {}


--==================================================
----------------------------------------------------------
-- Module:InfoboxPerson/Core
-- Module:InfoboxPerson/Core
-- Version 2
-- WikiAlborz Infobox Engine
-- Part 1
-- Version 1.1
--==================================================
-- Medal support added
----------------------------------------------------------


local html = mw.html
local html = mw.html


----------------------------------------------------
local data = require('Module:InfoboxPerson/Data')
-- LOAD DATA
----------------------------------------------------


local data = require("Module:InfoboxPerson/Data")
local lookup  = data.lookup
local layouts  = data.layouts
local groups  = data.groups
local defaults = data.defaults


local lookup = data.lookup or {}
----------------------------------------------------------
-- LOAD TEMPLATESTYLES
----------------------------------------------------------


local layouts = data.layouts or {}
local function loadStyles()


local sectionDefs = data.sections or {}
    return mw.getCurrentFrame():extensionTag{
        name = 'templatestyles',
        args = {
            src = 'Module:InfoboxPerson/styles.css'
        }
    }


end


 
----------------------------------------------------------
----------------------------------------------------
-- NORMALIZE
-- NORMALIZE
----------------------------------------------------
----------------------------------------------------------


local function normalize(value)
local function normalize(value)


if value == nil then
    if value == nil then
return nil
        return nil
end
    end


value = mw.text.trim(tostring(value))
    value = tostring(value)


if value == "" then
    value = mw.text.trim(value)
return nil
end


return value
    if value == '' then
        return nil
    end


end
    -- Remove invisible Unicode characters
    value = mw.ustring.gsub(
        value,
        '[\226\128\139\226\128\141\239\187\191]',
        ''
    )
 
    if value == '' then
        return nil
    end


    return value


end


----------------------------------------------------
----------------------------------------------------------
-- CHECK VALUE
-- HAS VALUE
----------------------------------------------------
----------------------------------------------------------


local function hasValue(value)
local function hasValue(value)


return normalize(value) ~= nil
    return normalize(value) ~= nil


end
end
----------------------------------------------------------
-- MAKE TERM DISPLAY
----------------------------------------------------------


local function makeTerm(startDate, endDate)


    if hasValue(startDate) and hasValue(endDate) then
        return startDate .. " تا " .. endDate
    elseif hasValue(startDate) then
        return startDate .. " تاکنون"
    elseif hasValue(endDate) then
        return "تا " .. endDate
    end


----------------------------------------------------
    return nil
-- COLLECT ARGUMENTS
end
----------------------------------------------------


local function collectArgs(frame)
----------------------------------------------------------
-- READ ARGUMENT TABLE
----------------------------------------------------------


local args = {}
local function readArguments(source, target)


local function merge(source)
    if not source then
        return
    end


if not source then
    for key, value in pairs(source) do
return
end


for key, value in pairs(source) do
        key = normalize(key)
        value = normalize(value)


key = normalize(key)
        if key and value then
value = normalize(value)
            target[key] = value
        end


if key and value then
    end


args[key] = value
end


end
----------------------------------------------------------
-- COLLECT ARGUMENTS
----------------------------------------------------------


end
local function collectArgs(frame)
 
end
 
 
local parent = frame:getParent()


if parent then
    local args = {}
merge(parent.args)
end


    local parent = frame:getParent()


merge(frame.args)
    if parent then
        readArguments(parent.args, args)
    end


    readArguments(frame.args, args)


return args
    return args


end
end
 
----------------------------------------------------------
 
 
----------------------------------------------------
-- RESOLVE ALIASES
-- RESOLVE ALIASES
----------------------------------------------------
----------------------------------------------------------


local function resolveArgs(rawArgs)
local function resolveArgs(rawArgs)


local args = {}
    local args = {}
 
for key, value in pairs(rawArgs) do


local canonical = lookup[key]
    for key, value in pairs(rawArgs) do


if canonical then
        local canonical = lookup[key]


args[canonical] = value
        if canonical then
            args[canonical] = value
        else
            args[key] = value
        end


else
    end


args[key] = value
    return args
 
end
 
end
 
 
return args


end
end


 
----------------------------------------------------------
 
----------------------------------------------------
-- CREATE TABLE
----------------------------------------------------
 
local function createBox()
 
return html.create("table")
:addClass("infobox")
:addClass("person-infobox")
 
end
----------------------------------------------------
-- RESOLVE ARGUMENTS
----------------------------------------------------
 
local function resolveArgs(rawArgs)
 
local args = {}
 
 
for key, value in pairs(rawArgs) do
 
 
local canonical = lookup[key]
 
 
if canonical then
 
args[canonical] = value
 
else
 
args[key] = value
 
end
 
 
end
 
 
return args
 
end
 
 
 
----------------------------------------------------
-- CREATE INFOBOX
-- CREATE INFOBOX
----------------------------------------------------
----------------------------------------------------------


local function createBox()
local function createBox()


 
    return html.create('table')
local box = html.create("table")
        :addClass('infobox')
 
        :addClass('person-infobox')
 
box
:addClass("infobox")
:addClass("person-infobox")
 
 
return box


end
end


 
----------------------------------------------------------
 
----------------------------------------------------
-- ADD TITLE
-- ADD TITLE
----------------------------------------------------
----------------------------------------------------------
 
local function addTitle(box,args)
 
 
local title = args.name
 
 
if not hasValue(title) then
 
title = mw.title.getCurrentTitle().text


end
local function addTitle(box, args)


    local title = args.name


    if not hasValue(title) then
        title = mw.title.getCurrentTitle().text
    end


box:tag("tr")
    -- نمایش عنوان شهید در بالای جعبه
:tag("th")
    if args.status == "شهید" then
:attr("colspan","2")
        box:tag("tr")
:addClass("infobox-person-title")
            :tag("th")
:wikitext(title)
            :attr("colspan", "2")
            :css("background", "#b22222")
            :css("color", "white")
            :css("text-align", "center")
            :css("font-size", "120%")
            :css("padding", "8px")
            :wikitext("شهید")
    end


    box:tag('tr')
        :tag('th')
        :attr('colspan', '2')
        :addClass('person-infobox-title')
        :wikitext(title)


end
end


 
----------------------------------------------------------
 
----------------------------------------------------
-- ADD IMAGE
-- ADD IMAGE
----------------------------------------------------
----------------------------------------------------------
 
local function addImage(box,args)
 


if not hasValue(args.image) then
local function addImage(box, args)


return
    local image = args.image


end
    if not hasValue(image) then
        return
    end


    box:tag('tr')
        :tag('td')
        :attr('colspan', '2')
        :addClass('person-infobox-image')
        :wikitext(
            '[[File:' ..
            image ..
            '|' ..
            (defaults.image_size or '250px') ..
            ']]'
        )


    if hasValue(args.caption) then


box:tag("tr")
        box:tag('tr')
:tag("td")
            :tag('td')
:attr("colspan","2")
            :attr('colspan', '2')
:addClass("infobox-person-image")
            :addClass('person-infobox-caption')
:wikitext(
            :wikitext(args.caption)
"[[File:" ..
args.image ..
"|250px]]"
)
 
 
 
if hasValue(args.caption) then
 
 
box:tag("tr")
:tag("td")
:attr("colspan","2")
:addClass("infobox-person-caption")
:wikitext(args.caption)
 
 
end


    end


end
end
 
----------------------------------------------------------
 
 
----------------------------------------------------
-- ADD HEADER
-- ADD HEADER
----------------------------------------------------
----------------------------------------------------------
 
local function addHeader(box,title)


local function addHeader(box, title)


box:tag("tr")
    if not hasValue(title) then
:tag("th")
        return
:attr("colspan","2")
    end
:addClass("infobox-person-header")
:wikitext(title)


    box:tag('tr')
        :tag('th')
        :attr('colspan', '2')
        :addClass('person-infobox-header')
        :wikitext(title)


end
end


 
----------------------------------------------------------
 
----------------------------------------------------
-- ADD ROW
-- ADD ROW
----------------------------------------------------
----------------------------------------------------------


local function addRow(box,label,value)
local function addRow(box, label, value)


    if not hasValue(value) then
        return false
    end


if not hasValue(value) then
    local row = box:tag('tr')


return false
    row:tag('th')
        :addClass('person-infobox-label')
        :wikitext(label)


end
    row:tag('td')
        :addClass('person-infobox-value')
        :wikitext(value)


    return true


end


local row = box:tag("tr")
----------------------------------------------------------
 
-- ADD MEDALS
----------------------------------------------------------


row
local function addMedals(box, args)
:tag("th")
:addClass("infobox-person-label")
:wikitext(label)


    local medals = args.medaltemplates


    if not hasValue(medals) then
        medals = args.medals
    end


row
    if not hasValue(medals) then
:tag("td")
        return
:addClass("infobox-person-value")
    end
:wikitext(value)


    local frame = mw.getCurrentFrame()


    local content = frame:expandTemplate{
        title = 'Infobox medal templates',
        args = {
            medals = medals,
            title = args.medaltemplates_title or '',
            expand = 'yes'
        }
    }


return true
    box:tag('tr')
        :tag('td')
        :attr('colspan','2')
        :addClass('person-infobox-medals')
        :wikitext(content)


end
end
----------------------------------------------------
----------------------------------------------------------
-- RENDER LAYOUT
-- RENDER LAYOUT
----------------------------------------------------
----------------------------------------------------------


local function renderLayout(box, layout, args)
local function renderLayout(box, layout, args)


    if not layout then
        return
    end


if not layout then
    for i = 1, #layout do
 
return
 
end


        local item = layout[i]


        local label = item[1]
        local field = item[2]


for i = 1, #layout do
        --------------------------------------------------
        -- MEDAL TEMPLATES
        -- Render directly inside infobox
        --------------------------------------------------


        if field == "medaltemplates" then


local row = layout[i]
            if hasValue(args[field]) then
                addMedals(box, args)
            end


        else


local label = row[1]
            addRow(
local field = row[2]
                box,
                label,
                args[field]
            )


        end


addRow(
    end
box,
label,
args[field]
)
 
 
end
 


end
end


----------------------------------------------------------
-- CHECK GROUP TRIGGER
----------------------------------------------------------


local function checkTrigger(group, args)


----------------------------------------------------
    if not group.trigger then
-- RENDER SECTION
        return true
----------------------------------------------------
    end


local function renderSection(box, sectionName, args)
    for i = 1, #group.trigger do


        local field = group.trigger[i]


local section = sections[sectionName]
        if hasValue(args[field]) then
            return true
        end


    end


if not section then
    return false


return
end


end
----------------------------------------------------------
-- RENDER GROUP
----------------------------------------------------------


local function renderGroup(box, group, args)


    if not checkTrigger(group, args) then
        return
    end


local shouldShow = false
    if hasValue(group.title) then


        addHeader(
            box,
            group.title
        )


    end


if section.trigger then
    renderLayout(
 
        box,
 
        layouts[group.layout],
for i = 1, #section.trigger do
        args
    )


end
----------------------------------------------------------
-- RENDER ALL GROUPS
----------------------------------------------------------


local field = section.trigger[i]
local function renderGroups(box, args)


    for i = 1, #groups do


if hasValue(args[field]) then
        renderGroup(
 
            box,
shouldShow = true
            groups[i],
break
            args
 
        )
end
 
 
end
 
 
else
 
shouldShow = true
 
end
 
 
 
if not shouldShow then
 
return
 
end
 
 
 
addHeader(
box,
section.title
)
 
 
 
renderLayout(
box,
layouts[section.layout],
args
)


    end


end
end


----------------------------------------------------------
-- PREPARE ARGUMENTS
----------------------------------------------------------


local function prepareArgs(args)


----------------------------------------------------
    for key, value in pairs(defaults) do
-- RENDER ALL SECTIONS
----------------------------------------------------
 
local function renderSections(box,args)
 
 
if not data.sectionOrder then
 
return
 
end
 


        if args[key] == nil then
            args[key] = value
        end


for i = 1, #data.sectionOrder do
    end
 
 
local sectionName = data.sectionOrder[i]
 
 
renderSection(
box,
sectionName,
args
)
 
 
end


    return args


end
end


 
----------------------------------------------------------
 
----------------------------------------------------
-- RENDER INFOBOX
-- RENDER INFOBOX
----------------------------------------------------
----------------------------------------------------------


local function render(args)
local function render(args)


    local box = createBox()


local box = createBox()
    addTitle(
        box,
        args
    )


    addImage(
        box,
        args
    )


    renderGroups(
        box,
        args
    )


addTitle(
    -- Render medal templates inside the same infobox
box,
args
)


    return loadStyles()
        .. tostring(box)


end
----------------------------------------------------------
-- MAIN ENTRY POINT
----------------------------------------------------------


addImage(
function p.main(frame)
box,
args
)


    local rawArgs = collectArgs(frame)


    local args = resolveArgs(rawArgs)


renderSection(
    -- ساخت خودکار دوره مسئولیت
box,
    args.term_display  = makeTerm(args.term_start,  args.term_end)
"personal",
    args.term_display2 = makeTerm(args.term_start2, args.term_end2)
args
    args.term_display3 = makeTerm(args.term_start3, args.term_end3)
)


    args = prepareArgs(args)


    return render(args)


renderSection(
end
box,
"career",
args
)
 
 
 
renderSections(
box,
args
)
 
 
 
return loadStyles()
.. tostring(box)


----------------------------------------------------------
-- EXPORT
----------------------------------------------------------


end
return p

نسخهٔ کنونی تا ‏۱۹ ژوئیهٔ ۲۰۲۶، ساعت ۱۲:۲۶

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

local p = {}

----------------------------------------------------------
-- Module:InfoboxPerson/Core
-- WikiAlborz Infobox Engine
-- Version 1.1
-- Medal support added
----------------------------------------------------------

local html = mw.html

local data = require('Module:InfoboxPerson/Data')

local lookup   = data.lookup
local layouts  = data.layouts
local groups   = data.groups
local defaults = data.defaults

----------------------------------------------------------
-- LOAD TEMPLATESTYLES
----------------------------------------------------------

local function loadStyles()

    return mw.getCurrentFrame():extensionTag{
        name = 'templatestyles',
        args = {
            src = 'Module:InfoboxPerson/styles.css'
        }
    }

end

----------------------------------------------------------
-- NORMALIZE
----------------------------------------------------------

local function normalize(value)

    if value == nil then
        return nil
    end

    value = tostring(value)

    value = mw.text.trim(value)

    if value == '' then
        return nil
    end

    -- Remove invisible Unicode characters
    value = mw.ustring.gsub(
        value,
        '[\226\128\139\226\128\141\239\187\191]',
        ''
    )

    if value == '' then
        return nil
    end

    return value

end

----------------------------------------------------------
-- HAS VALUE
----------------------------------------------------------

local function hasValue(value)

    return normalize(value) ~= nil

end
----------------------------------------------------------
-- MAKE TERM DISPLAY
----------------------------------------------------------

local function makeTerm(startDate, endDate)

    if hasValue(startDate) and hasValue(endDate) then
        return startDate .. " تا " .. endDate
    elseif hasValue(startDate) then
        return startDate .. " تاکنون"
    elseif hasValue(endDate) then
        return "تا " .. endDate
    end

    return nil
end

----------------------------------------------------------
-- READ ARGUMENT TABLE
----------------------------------------------------------

local function readArguments(source, target)

    if not source then
        return
    end

    for key, value in pairs(source) do

        key = normalize(key)
        value = normalize(value)

        if key and value then
            target[key] = value
        end

    end

end

----------------------------------------------------------
-- COLLECT ARGUMENTS
----------------------------------------------------------

local function collectArgs(frame)

    local args = {}

    local parent = frame:getParent()

    if parent then
        readArguments(parent.args, args)
    end

    readArguments(frame.args, args)

    return args

end
----------------------------------------------------------
-- RESOLVE ALIASES
----------------------------------------------------------

local function resolveArgs(rawArgs)

    local args = {}

    for key, value in pairs(rawArgs) do

        local canonical = lookup[key]

        if canonical then
            args[canonical] = value
        else
            args[key] = value
        end

    end

    return args

end

----------------------------------------------------------
-- CREATE INFOBOX
----------------------------------------------------------

local function createBox()

    return html.create('table')
        :addClass('infobox')
        :addClass('person-infobox')

end

----------------------------------------------------------
-- ADD TITLE
----------------------------------------------------------

local function addTitle(box, args)

    local title = args.name

    if not hasValue(title) then
        title = mw.title.getCurrentTitle().text
    end

    -- نمایش عنوان شهید در بالای جعبه
    if args.status == "شهید" then
        box:tag("tr")
            :tag("th")
            :attr("colspan", "2")
            :css("background", "#b22222")
            :css("color", "white")
            :css("text-align", "center")
            :css("font-size", "120%")
            :css("padding", "8px")
            :wikitext("شهید")
    end

    box:tag('tr')
        :tag('th')
        :attr('colspan', '2')
        :addClass('person-infobox-title')
        :wikitext(title)

end

----------------------------------------------------------
-- ADD IMAGE
----------------------------------------------------------

local function addImage(box, args)

    local image = args.image

    if not hasValue(image) then
        return
    end

    box:tag('tr')
        :tag('td')
        :attr('colspan', '2')
        :addClass('person-infobox-image')
        :wikitext(
            '[[File:' ..
            image ..
            '|' ..
            (defaults.image_size or '250px') ..
            ']]'
        )

    if hasValue(args.caption) then

        box:tag('tr')
            :tag('td')
            :attr('colspan', '2')
            :addClass('person-infobox-caption')
            :wikitext(args.caption)

    end

end
----------------------------------------------------------
-- ADD HEADER
----------------------------------------------------------

local function addHeader(box, title)

    if not hasValue(title) then
        return
    end

    box:tag('tr')
        :tag('th')
        :attr('colspan', '2')
        :addClass('person-infobox-header')
        :wikitext(title)

end

----------------------------------------------------------
-- ADD ROW
----------------------------------------------------------

local function addRow(box, label, value)

    if not hasValue(value) then
        return false
    end

    local row = box:tag('tr')

    row:tag('th')
        :addClass('person-infobox-label')
        :wikitext(label)

    row:tag('td')
        :addClass('person-infobox-value')
        :wikitext(value)

    return true

end

----------------------------------------------------------
-- ADD MEDALS
----------------------------------------------------------

local function addMedals(box, args)

    local medals = args.medaltemplates

    if not hasValue(medals) then
        medals = args.medals
    end

    if not hasValue(medals) then
        return
    end

    local frame = mw.getCurrentFrame()

    local content = frame:expandTemplate{
        title = 'Infobox medal templates',
        args = {
            medals = medals,
            title = args.medaltemplates_title or '',
            expand = 'yes'
        }
    }

    box:tag('tr')
        :tag('td')
        :attr('colspan','2')
        :addClass('person-infobox-medals')
        :wikitext(content)

end
----------------------------------------------------------
-- RENDER LAYOUT
----------------------------------------------------------

local function renderLayout(box, layout, args)

    if not layout then
        return
    end

    for i = 1, #layout do

        local item = layout[i]

        local label = item[1]
        local field = item[2]

        --------------------------------------------------
        -- MEDAL TEMPLATES
        -- Render directly inside infobox
        --------------------------------------------------

        if field == "medaltemplates" then

            if hasValue(args[field]) then
                addMedals(box, args)
            end

        else

            addRow(
                box,
                label,
                args[field]
            )

        end

    end

end

----------------------------------------------------------
-- CHECK GROUP TRIGGER
----------------------------------------------------------

local function checkTrigger(group, args)

    if not group.trigger then
        return true
    end

    for i = 1, #group.trigger do

        local field = group.trigger[i]

        if hasValue(args[field]) then
            return true
        end

    end

    return false

end

----------------------------------------------------------
-- RENDER GROUP
----------------------------------------------------------

local function renderGroup(box, group, args)

    if not checkTrigger(group, args) then
        return
    end

    if hasValue(group.title) then

        addHeader(
            box,
            group.title
        )

    end

    renderLayout(
        box,
        layouts[group.layout],
        args
    )

end
----------------------------------------------------------
-- RENDER ALL GROUPS
----------------------------------------------------------

local function renderGroups(box, args)

    for i = 1, #groups do

        renderGroup(
            box,
            groups[i],
            args
        )

    end

end

----------------------------------------------------------
-- PREPARE ARGUMENTS
----------------------------------------------------------

local function prepareArgs(args)

    for key, value in pairs(defaults) do

        if args[key] == nil then
            args[key] = value
        end

    end

    return args

end

----------------------------------------------------------
-- RENDER INFOBOX
----------------------------------------------------------

local function render(args)

    local box = createBox()

    addTitle(
        box,
        args
    )

    addImage(
        box,
        args
    )

    renderGroups(
        box,
        args
    )

    -- Render medal templates inside the same infobox

    return loadStyles()
        .. tostring(box)

end
----------------------------------------------------------
-- MAIN ENTRY POINT
----------------------------------------------------------

function p.main(frame)

    local rawArgs = collectArgs(frame)

    local args = resolveArgs(rawArgs)

    -- ساخت خودکار دوره مسئولیت
    args.term_display  = makeTerm(args.term_start,  args.term_end)
    args.term_display2 = makeTerm(args.term_start2, args.term_end2)
    args.term_display3 = makeTerm(args.term_start3, args.term_end3)

    args = prepareArgs(args)

    return render(args)

end

----------------------------------------------------------
-- EXPORT
----------------------------------------------------------

return p