모듈:ImageSwitch

모니터링 (토론 | 기여)님의 2025년 4월 8일 (화) 09:11 판

이 모듈에 대한 설명문서는 모듈:ImageSwitch/설명문서에서 만들 수 있습니다

local p = {}

function escape_html(text)
    -- HTML 특수문자 이스케이프
    return text
        :gsub("&", "&")
        :gsub("<", "&lt;")
        :gsub(">", "&gt;")
        :gsub("\"", "&quot;")
        :gsub("'", "&#39;")
end

function p.main(frame)
    local args = frame:getParent().args
    local light = args[1] or ''
    local dark = args[2] or ''
    local width = args[3] or ''
    local alt = escape_html(args['alt'] or '')
    local link = escape_html(args['link'] or '')
    local class = escape_html(args['class'] or '')
    local style = escape_html(args['style'] or '')

    -- 필수 항목 검증
    if light == '' then
        return '<strong class="error">라이트모드 이미지가 필요합니다.</strong>'
    end

    -- 경로 변환 (미디어위키 내장 함수 사용)
    local lighturl = frame:callParserFunction('filepath', light)
    local darkurl = ''
    if dark ~= '' then
        darkurl = frame:callParserFunction('filepath', dark)
    end

    -- <img> 태그 구성
    local imgTag = '<img src="' .. escape_html(lighturl) .. '"'
    if width ~= '' then imgTag = imgTag .. ' width="' .. escape_html(width) .. '"' end
    if alt ~= '' then imgTag = imgTag .. ' alt="' .. alt .. '"' end
    if class ~= '' then imgTag = imgTag .. ' class="' .. class .. '"' end
    if style ~= '' then imgTag = imgTag .. ' style="' .. style .. '"' end
    imgTag = imgTag .. '>'

    if link ~= '' then
        imgTag = '<a href="/wiki/' .. link .. '">' .. imgTag .. '</a>'
    end

    -- 최종 picture 구성
    local html = '<div class="image-switch-wrapper"><picture>'
    if darkurl ~= '' then
        html = html .. '<source srcset="' .. escape_html(darkurl) .. '" media="(prefers-color-scheme: dark)">'
    end
    html = html .. imgTag .. '</picture></div>'

    return html
end

return p