Tidak ada ringkasan suntingan
Tidak ada ringkasan suntingan
Baris 7: Baris 7:
end
end


local function render(args)
local function render(args, frame)
     local tiles = mw.text.trim(args[1] or '')
     local tiles = mw.text.trim(args[1] or '')
     local size = tonumber(args.size or '32')
     local size = tonumber(args.size or '32')
Baris 97: Baris 97:
             end
             end


             local title = mw.title.new('File:' .. filename)
             local fileTitle = mw.title.new('File:' .. filename)


             if not title or not title.exists then
             if not fileTitle or not fileTitle.exists then
                 return errorMessage('Missing image: ' .. filename)
                 return errorMessage('Missing image: ' .. filename)
             end
             end
Baris 106: Baris 106:
                 output:wikitext(' ')
                 output:wikitext(' ')
             end
             end
            local fileMarkup = frame:preprocess(
                '[[File:' .. filename ..
                '|frameless|' .. tostring(size) ..
                'px|alt=' .. label ..
                '|link=]]'
            )


             local tile = mw.html.create('span')
             local tile = mw.html.create('span')
Baris 111: Baris 118:
                 :css('display', 'inline-block')
                 :css('display', 'inline-block')
                 :css('vertical-align', 'middle')
                 :css('vertical-align', 'middle')
                 :css('line-height', '0')
                 :css('margin-right', '2px')
 
            local imageUrl = mw.util.getUrl(
                'Special:Redirect/file/' .. filename
            )
 
            local img = mw.html.create('img')
                :attr('src', imageUrl)
                :attr('alt', label)
                :attr('title', label)
                :css('width', tostring(size) .. 'px')
                :css('height', 'auto')
                :css('max-width', '100%')


             tile:node(img)
             tile:wikitext(fileMarkup)
             output:node(tile)
             output:node(tile)


Baris 144: Baris 139:
     end
     end


     return render(args)
     return render(args, frame)
end
end


return p
return p

Revisi per 12 Oktober 2026 17.22

Dokumentasi untuk modul ini dapat dibuat di Modul:MahjongTile/doc


local p = {}

local function errorMessage(message)
    return '<span class="error">MahjongTile: ' ..
        mw.text.nowiki(message) .. '</span>'
end

local function render(args, frame)
    local tiles = mw.text.trim(args[1] or '')
    local size = tonumber(args.size or '32')
    local variant = mw.text.trim(args.variant or 'riichi'):lower()

    if tiles == '' then
        return errorMessage('Please supply tile notation.')
    end

    if variant ~= 'riichi' and variant ~= 'mcr' then
        return errorMessage('variant must be riichi or mcr.')
    end

    if not size or size < 12 or size > 120 then
        return errorMessage('size must be between 12 and 120 pixels.')
    end

    tiles = tiles:gsub('%s+', '')

    local output = mw.html.create('span')
        :addClass('mahjong-tiles')
        :css('white-space', 'normal')

    local suitNames = {
        m = 'characters',
        p = 'circles',
        s = 'bamboo'
    }

    local honorNames = {
        [1] = 'East wind',
        [2] = 'South wind',
        [3] = 'West wind',
        [4] = 'North wind',
        [5] = 'White dragon',
        [6] = 'Green dragon',
        [7] = 'Red dragon'
    }

    local position = 1
    local tileCount = 0

    while position <= #tiles do
        local rest = tiles:sub(position)
        local group, suit = rest:match('^(%d+)([mpszf])')

        if not group then
            return errorMessage(
                'Invalid notation near "' .. rest .. '".'
            )
        end

        for i = 1, #group do
            local n = tonumber(group:sub(i, i))
            local valid = false

            if suit == 'm' or suit == 'p' or suit == 's' then
                valid = (n >= 1 and n <= 9) or n == 0
            elseif suit == 'z' then
                valid = n >= 1 and n <= 7
            elseif suit == 'f' then
                valid = n >= 1 and n <= 8
            end

            if not valid then
                return errorMessage(
                    'Invalid tile number ' .. n ..
                    ' for suit ' .. suit .. '.'
                )
            end

            if suit == 'f' and variant == 'riichi' then
                return errorMessage(
                    'Flower tiles require variant=mcr.'
                )
            end

            local filename = 'Mahjong-' .. n .. suit .. '.svg'
            local label

            if suit == 'z' then
                label = honorNames[n]
            elseif suit == 'f' then
                label = 'Flower or season ' .. n
            elseif n == 0 then
                label = 'Red five, ' .. suitNames[suit]
            else
                label = n .. ' of ' .. suitNames[suit]
            end

            local fileTitle = mw.title.new('File:' .. filename)

            if not fileTitle or not fileTitle.exists then
                return errorMessage('Missing image: ' .. filename)
            end

            if tileCount > 0 then
                output:wikitext('&nbsp;')
            end

            local fileMarkup = frame:preprocess(
                '[[File:' .. filename ..
                '|frameless|' .. tostring(size) ..
                'px|alt=' .. label ..
                '|link=]]'
            )

            local tile = mw.html.create('span')
                :addClass('mahjong-tile')
                :css('display', 'inline-block')
                :css('vertical-align', 'middle')
                :css('margin-right', '2px')

            tile:wikitext(fileMarkup)
            output:node(tile)

            tileCount = tileCount + 1
        end

        position = position + #group + 1
    end

    return tostring(output)
end

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

    if frame:getParent() then
        args = frame:getParent().args
    end

    return render(args, frame)
end

return p