메뉴 여닫기
환경 설정 메뉴 여닫기
개인 메뉴 여닫기
로그인하지 않음
만약 지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.

💬 같이 떠들 사람? 노바위키 디스코드

모듈:요약정보/군사

노바위키

이 모듈에 대한 설명문서는 모듈:요약정보/군사/설명문서에서 만들 수 있습니다

local p = {}

local MOBILE_VISIBLE = 5

local function clean(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 parent = frame:getParent()
    local source = frame.args

    if parent then
        for _, value in pairs(parent.args) do
            if clean(value) then
                source = parent.args
                break
            end
        end
    end

    local args = {}

    for key, value in pairs(source) do
        if type(key) == 'string' then
            local name = clean(key)
            local content = clean(value)

            if name and content then
                args[name] = content
            end
        end
    end

    return args
end

local function first(args, keys)
    for _, key in ipairs(keys) do
        if args[key] then
            return args[key]
        end
    end

    return nil
end

local function applyColor(element, property, value)
    if value then
        element:css(property, value)
    end

    return element
end

local function imageSize(value)
    if not value then
        return '250px'
    end

    local number = tonumber(
        value:match('^(%d+)%s*px$')
        or value:match('^(%d+)$')
    )

    if number then
        number = math.max(80, math.min(number, 600))
        return tostring(number) .. 'px'
    end

    return '250px'
end

local function imageMarkup(filename, size)
    if filename:match('^%[%[') then
        return filename
    end

    filename = filename
        :gsub('^파일:', '')
        :gsub('^그림:', '')
        :gsub('^[Ff]ile:', '')
        :gsub('^[Ii]mage:', '')

    return '[[파일:' .. filename .. '|' .. size .. ']]'
end

local GROUPS = {
    {
        title = nil,
        fields = {
            {'창립일', '창립일'},
            {'국적', '국적'},
            {'편제', '편제'},
            {'충원방식', '충원방식'},
            {'징집 연령', '징집 연령'},
            {'병력', '상비군'},
            {'예비군', '예비군'}
        }
    },
    {
        title = '지휘체계',
        fields = {
            {'명령체계', '명령체계'},
            {'본부', '본부'},
            {'통수권자', '통수권자'},
            {'국방장관', '국방장관'},
            {'총사령관', '총사령관'},
            {'합참의장', '합참의장'},
            {'연합참모장', '연합참모장'},
            {'총참모장', '총참모장'}
        }
    },
    {
        title = '군사비',
        fields = {
            {'예산', '예산'},
            {'GDP 대비', 'GDP 대비'},
            {'정부재정대비', '정부재정 대비'},
            {'군사비 기타', '기타'}
        }
    },
    {
        title = '방위산업',
        fields = {
            {'국내 공급', '국내 공급'},
            {'해외 공급', '해외 공급'}
        }
    },
    {
        title = '함께 읽기',
        fields = {
            {'관련정보', '관련정보'},
            {'웹사이트', '웹사이트'}
        }
    }
}

local SPECIAL_KEYS = {
    '명칭',
    '원어 명칭',
    '그림',
    '그림1',
    '그림2',
    '그림3',
    '그림크기',
    '그림1 크기',
    '그림2크기',
    '그림2 크기',
    '그림3 크기',
    '설명',
    '설명2',
    '설명3',
    '그림1 설명',
    '그림2 설명',
    '그림3 설명',
    '항목순서',
    '틀',
    '표배경색',
    '표글씨색',
    '테두리색',
    '제목배경색',
    '제목글씨색',
    '배경색상코드',
    '글씨색상코드',
    '항목배경색',
    '항목글씨색',
    '값배경색',
    '값글씨색',
    '이미지배경색',
    '이미지글씨색',
    '추가정보배경색',
    '추가정보글씨색',
    '구분배경색',
    '구분글씨색',
    '하단배경색',
    '하단글씨색',
    '지휘체계',
    '군사비',
    '방위산업',
    '함께 읽기'
}

local function buildEntries(args)
    local entries = {}
    local handled = {}

    for _, key in ipairs(SPECIAL_KEYS) do
        handled[key] = true
    end

    for _, group in ipairs(GROUPS) do
        local present = {}

        for _, spec in ipairs(group.fields) do
            local key = spec[1]
            local label = spec[2]

            handled[key] = true

            if args[key] then
                present[#present + 1] = {
                    kind = 'field',
                    name = label,
                    value = args[key]
                }
            end
        end

        if #present > 0 then
            if group.title then
                entries[#entries + 1] = {
                    kind = 'section',
                    name = group.title
                }
            end

            for _, item in ipairs(present) do
                entries[#entries + 1] = item
            end
        end
    end

    local extraKeys = {}

    for key, _ in pairs(args) do
        if not handled[key] then
            extraKeys[#extraKeys + 1] = key
        end
    end

    table.sort(extraKeys)

    if #extraKeys > 0 then
        entries[#entries + 1] = {
            kind = 'section',
            name = '기타 정보'
        }

        for _, key in ipairs(extraKeys) do
            entries[#entries + 1] = {
                kind = 'field',
                name = key,
                value = args[key]
            }
        end
    end

    return entries
end

local function addFieldRow(target, item, args)
    local row = target:tag('tr')

    local label = row:tag('th')
        :attr('scope', 'row')
        :addClass('nw-summary-label')
        :wikitext(item.name)

    applyColor(
        label,
        'background-color',
        args['항목배경색']
    )

    applyColor(
        label,
        'color',
        args['항목글씨색']
    )

    applyColor(
        label,
        'border-color',
        args['테두리색']
    )

    local value = row:tag('td')
        :addClass('nw-summary-value')
        :wikitext(item.value)

    applyColor(
        value,
        'background-color',
        args['값배경색']
    )

    applyColor(
        value,
        'color',
        args['값글씨색']
    )

    applyColor(
        value,
        'border-color',
        args['테두리색']
    )
end

local function addSectionRow(target, item, args)
    local cell = target:tag('tr')
        :addClass('nw-summary-group-row')
        :tag('th')
        :attr('colspan', '2')
        :addClass('nw-summary-group-heading')
        :wikitext(item.name)

    applyColor(
        cell,
        'background-color',
        args['구분배경색']
    )

    applyColor(
        cell,
        'color',
        args['구분글씨색']
    )

    applyColor(
        cell,
        'border-color',
        args['테두리색']
    )
end

local function addEntry(target, item, args)
    if item.kind == 'section' then
        addSectionRow(target, item, args)
    else
        addFieldRow(target, item, args)
    end
end

local function addImage(root, args, filename, size, caption, index)
    if not filename then
        return
    end

    local cell = root:tag('tr')
        :addClass('nw-summary-image-row')
        :tag('td')
        :attr('colspan', '2')
        :addClass('nw-summary-image')
        :addClass('nw-summary-image-' .. index)

    applyColor(
        cell,
        'background-color',
        args['이미지배경색']
    )

    applyColor(
        cell,
        'color',
        args['이미지글씨색']
    )

    applyColor(
        cell,
        'border-color',
        args['테두리색']
    )

    cell:tag('div')
        :addClass('nw-summary-image-file')
        :wikitext(
            imageMarkup(filename, imageSize(size))
        )

    if caption then
        local captionBox = cell:tag('div')
            :addClass('nw-summary-image-caption')
            :wikitext(caption)

        applyColor(
            captionBox,
            'color',
            args['이미지글씨색']
        )
    end
end

local function splitEntries(entries)
    local visible = {}
    local extra = {}
    local fieldCount = 0

    for _, item in ipairs(entries) do
        if item.kind == 'field' then
            fieldCount = fieldCount + 1
        end

        if fieldCount <= MOBILE_VISIBLE then
            visible[#visible + 1] = item
        else
            extra[#extra + 1] = item
        end
    end

    return visible, extra, fieldCount
end

local function addExtra(root, entries, args, count)
    if #entries == 0 then
        return
    end

    local cell = root:tag('tr')
        :addClass('nw-summary-extra-row')
        :tag('td')
        :attr('colspan', '2')
        :addClass('nw-summary-extra-cell')

    local box = cell:tag('div')
        :addClass('nw-summary-extra-box')
        :addClass('mw-collapsible')
        :addClass('mw-collapsed')
        :attr('data-expandtext', '펼치기')
        :attr('data-collapsetext', '접기')

    local head = box:tag('div')
        :addClass('nw-summary-extra-head')
        :wikitext(
            '추가 정보 (' ..
            tostring(count - MOBILE_VISIBLE) ..
            '개)'
        )

    applyColor(
        head,
        'background-color',
        args['추가정보배경색']
    )

    applyColor(
        head,
        'color',
        args['추가정보글씨색']
    )

    local content = box:tag('div')
        :addClass('mw-collapsible-content')
        :addClass('nw-summary-extra-content')

    local inner = content:tag('table')
        :addClass('nw-summary-extra-table')
        :addClass('citizen-table-nowrap')

    for _, item in ipairs(entries) do
        addEntry(inner, item, args)
    end
end

local function addFooter(root, frame, args)
    local templateName = args['틀']

    if not templateName then
        return
    end

    local success, expanded = pcall(function()
        return frame:expandTemplate({
            title = templateName
        })
    end)

    if success and clean(expanded) then
        local cell = root:tag('tr')
            :tag('td')
            :attr('colspan', '2')
            :addClass('nw-summary-footer')
            :wikitext(expanded)

        applyColor(
            cell,
            'background-color',
            args['하단배경색']
        )

        applyColor(
            cell,
            'color',
            args['하단글씨색']
        )
    end
end

function p.main(frame)
    local args = getArgs(frame)

    local title =
        args['명칭']
        or mw.title.getCurrentTitle().text

    local original = args['원어 명칭']

    local root = mw.html.create('table')
        :addClass('nw-summary-profile')
        :addClass('nw-summary-military')
        :addClass('citizen-table-nowrap')

    applyColor(
        root,
        'background-color',
        args['표배경색']
    )

    applyColor(
        root,
        'color',
        args['표글씨색']
    )

    applyColor(
        root,
        'border-color',
        args['테두리색']
    )

    local heading = root:tag('tr')
        :tag('th')
        :attr('colspan', '2')
        :addClass('nw-summary-heading')

    applyColor(
        heading,
        'background-color',
        args['제목배경색']
            or args['배경색상코드']
    )

    applyColor(
        heading,
        'color',
        args['제목글씨색']
            or args['글씨색상코드']
    )

    heading:tag('div')
        :addClass('nw-summary-title-main')
        :wikitext(title)

    if original then
        heading:tag('div')
            :addClass('nw-summary-title-sub')
            :wikitext(original)
    end

    addImage(
        root,
        args,
        args['그림1'] or args['그림'],
        args['그림1 크기'] or args['그림크기'],
        args['그림1 설명'] or args['설명'],
        1
    )

    addImage(
        root,
        args,
        args['그림2'],
        args['그림2 크기'] or args['그림2크기'],
        args['그림2 설명'] or args['설명2'],
        2
    )

    addImage(
        root,
        args,
        args['그림3'],
        args['그림3 크기'],
        args['그림3 설명'] or args['설명3'],
        3
    )

    local entries = buildEntries(args)

    local visible, extra, count = splitEntries(entries)

    for _, item in ipairs(visible) do
        addEntry(root, item, args)
    end

    addExtra(root, extra, args, count)

    addFooter(root, frame, args)

    return tostring(root)
end

return p