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)
    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 title = mw.title.new('File:' .. filename)

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

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

            local tile = mw.html.create('span')
                :addClass('mahjong-tile')
                :css('display', 'inline-block')
                :css('vertical-align', 'middle')
                :css('line-height', '0')

            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)
            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)
end

return p