모듈:ImageSwitch: 두 판 사이의 차이

편집 요약 없음
편집 요약 없음
1번째 줄: 1번째 줄:
local p = {}
local p = {}


-- HTML 특수문자 이스케이프
-- HTML 특수문자 이스케이프 함수
local function escape_html(text)
local function escape_html(text)
     return text
     return text
14번째 줄: 14번째 줄:
     local args = frame:getParent().args
     local args = frame:getParent().args


     -- 필수 인자
     -- 필수: 라이트모드 이미지
     local light = args[1] or ''
     local light = args[1] or ''
    if light == '' then
        return '<strong class="error">라이트모드 이미지가 필요합니다.</strong>'
    end
    -- 선택: 다크모드 이미지
     local dark = args[2] or ''
     local dark = args[2] or ''
     local width = escape_html(args[3] or '')
     local width = escape_html(args[3] or '')


    -- 선택 인자
     local alt = escape_html(args['alt'] or '')
     local alt = escape_html(args['alt'] or '')
     local raw_link = args['link'] or ''
     local raw_link = args['link']
     local class = escape_html(args['class'] or '')
     local class = escape_html(args['class'] or '')
     local style = escape_html(args['style'] or '')
     local style = escape_html(args['style'] or '')


    if light == '' then
     -- 이미지 경로 변환
        return '<strong class="error">라이트모드 이미지가 필요합니다.</strong>'
    end
 
     -- 파일 경로 처리
     local lighturl = escape_html(frame:callParserFunction('filepath', light))
     local lighturl = escape_html(frame:callParserFunction('filepath', light))
     local darkurl = ''
     local darkurl = ''
     if dark and mw.text.trim(dark) ~= '' then
     if dark ~= '' then
         darkurl = escape_html(frame:callParserFunction('filepath', dark))
         darkurl = escape_html(frame:callParserFunction('filepath', dark))
     end
     end


     -- 링크 인코딩 및 내부링크 처리
     -- 링크 처리
     local link = ''
     local link = nil
     if raw_link ~= '' then
     if raw_link ~= nil then
         link = '/wiki/index.php/' .. mw.uri.encode(raw_link, 'WIKI')
        if raw_link ~= '' then
            link = '/wiki/index.php/' .. mw.uri.encode(raw_link, 'WIKI')
        else
            link = ''  -- link= 명시했지만 비어 있음 → 링크 없음
        end
    else
         link = '/wiki/index.php/파일:' .. mw.uri.encode(light, 'WIKI')
     end
     end


     -- 이미지 구성
     -- 이미지 태그 생성 함수
    local html = {}
    table.insert(html, '<div class="image-switch-wrapper">')
 
     local function make_img(src, img_class)
     local function make_img(src, img_class)
         local tag = '<img src="' .. src .. '" class="' .. img_class
         local tag = '<img src="' .. src .. '" class="' .. img_class
60번째 줄: 63번째 줄:
     local dark_img = darkurl ~= '' and make_img(darkurl, 'dark-mode-img') or ''
     local dark_img = darkurl ~= '' and make_img(darkurl, 'dark-mode-img') or ''


    -- 최종 출력
    local html = {}
    table.insert(html, '<div class="image-switch-wrapper">')
     if link ~= '' then
     if link ~= '' then
         table.insert(html, '<a href="' .. link .. '">' .. light_img .. dark_img .. '</a>')
         table.insert(html, '<a href="' .. link .. '">' .. light_img .. dark_img .. '</a>')
65번째 줄: 71번째 줄:
         table.insert(html, light_img .. dark_img)
         table.insert(html, light_img .. dark_img)
     end
     end
    table.insert(html, '</div>')


    table.insert(html, '</div>')
     return table.concat(html)
     return table.concat(html)
end
end


return p
return p

2025년 4월 8일 (화) 09:30 판

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

local p = {}

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

function p.main(frame)
    local args = frame:getParent().args

    -- 필수: 라이트모드 이미지
    local light = args[1] or ''
    if light == '' then
        return '<strong class="error">라이트모드 이미지가 필요합니다.</strong>'
    end

    -- 선택: 다크모드 이미지
    local dark = args[2] or ''
    local width = escape_html(args[3] or '')

    local alt = escape_html(args['alt'] or '')
    local raw_link = args['link']
    local class = escape_html(args['class'] or '')
    local style = escape_html(args['style'] or '')

    -- 이미지 경로 변환
    local lighturl = escape_html(frame:callParserFunction('filepath', light))
    local darkurl = ''
    if dark ~= '' then
        darkurl = escape_html(frame:callParserFunction('filepath', dark))
    end

    -- 링크 처리
    local link = nil
    if raw_link ~= nil then
        if raw_link ~= '' then
            link = '/wiki/index.php/' .. mw.uri.encode(raw_link, 'WIKI')
        else
            link = ''  -- link= 명시했지만 비어 있음 → 링크 없음
        end
    else
        link = '/wiki/index.php/파일:' .. mw.uri.encode(light, 'WIKI')
    end

    -- 이미지 태그 생성 함수
    local function make_img(src, img_class)
        local tag = '<img src="' .. src .. '" class="' .. img_class
        if class ~= '' then tag = tag .. ' ' .. class end
        tag = tag .. '"'
        if width ~= '' then tag = tag .. ' width="' .. width .. '"' end
        if alt ~= '' then tag = tag .. ' alt="' .. alt .. '"' end
        if style ~= '' then tag = tag .. ' style="' .. style .. '"' end
        tag = tag .. '>'
        return tag
    end

    local light_img = make_img(lighturl, 'light-mode-img')
    local dark_img = darkurl ~= '' and make_img(darkurl, 'dark-mode-img') or ''

    -- 최종 출력
    local html = {}
    table.insert(html, '<div class="image-switch-wrapper">')
    if link ~= '' then
        table.insert(html, '<a href="' .. link .. '">' .. light_img .. dark_img .. '</a>')
    else
        table.insert(html, light_img .. dark_img)
    end
    table.insert(html, '</div>')

    return table.concat(html)
end

return p