Tidak ada ringkasan suntingan
Tidak ada ringkasan suntingan
 
(5 revisi perantara oleh pengguna yang sama tidak ditampilkan)
Baris 4: Baris 4:
local function errorMessage(message)
local function errorMessage(message)
     return '<span class="error">MahjongTile: ' ..
     return '<span class="error">MahjongTile: ' ..
         message .. '</span>'
         mw.text.nowiki(message) .. '</span>'
end
end


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


     if tiles == '' then
     if tiles == '' then
Baris 17: Baris 15:
     end
     end


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


     local width = tonumber(size)
     -- Ignore whitespace in the notation.
     if not width or width < 12 or width > 120 then
    tiles = tiles:gsub('%s+', '')
         return errorMessage('size must be 12–120 pixels.')
 
     end
     local suitNames = {
         m = 'characters',
        p = 'circles',
        s = 'bamboo'
     }


     tiles = tiles:gsub('%s+', '')
     local honorNames = {
        [1] = 'East wind',
        [2] = 'South wind',
        [3] = 'West wind',
        [4] = 'North wind',
        [5] = 'White dragon',
        [6] = 'Green dragon',
        [7] = 'Red dragon'
    }


     local output = {}
     local output = {}
Baris 33: Baris 43:
     while position <= #tiles do
     while position <= #tiles do
         local rest = tiles:sub(position)
         local rest = tiles:sub(position)
         local group, suit = rest:match('^(%d+)([mpszf])')
 
        -- Read a sequence of digits followed by its suit.
         local group, suit = rest:match('^(%d+)([mpszfx])')


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


        -- Validate and render each tile in the group.
         for i = 1, #group do
         for i = 1, #group do
             local n = tonumber(group:sub(i, i))
             local n = tonumber(group:sub(i, i))
             local valid = false
             local valid = false


             if suit == 'm' or suit == 'p' or suit == 's' then
             if suit == 'x' then
                -- The tile back is represented by 1x only.
                valid = n == 1 and #group == 1
 
            elseif suit == 'm' or suit == 'p' or suit == 's' then
                -- Numbered suits, including red fives (0).
                 valid = (n >= 1 and n <= 9) or n == 0
                 valid = (n >= 1 and n <= 9) or n == 0
             elseif suit == 'z' then
             elseif suit == 'z' then
                -- Four winds and three dragons.
                 valid = n >= 1 and n <= 7
                 valid = n >= 1 and n <= 7
             elseif suit == 'f' then
             elseif suit == 'f' then
                -- Four flowers and four seasons.
                 valid = n >= 1 and n <= 8
                 valid = n >= 1 and n <= 8
             end
             end


             if not valid then
             if not valid then
                return errorMessage('Invalid tile number ' ..
                    n .. ' for suit ' .. suit .. '.')
            end
            if suit == 'f' and variant == 'riichi' then
                 return errorMessage(
                 return errorMessage(
                     'Flower tiles require variant=mcr.')
                     'Invalid tile number ' .. n ..
                    ' for suit ' .. suit .. '.'
                )
             end
             end


Baris 65: Baris 85:
             local label
             local label


             if suit == 'z' then
             if suit == 'x' then
                 local honors = {
                label = 'Mahjong tile back'
                     [1] = 'East wind',
 
                     [2] = 'South wind',
            elseif suit == 'z' then
                     [3] = 'West wind',
                label = honorNames[n]
                     [4] = 'North wind',
 
                     [5] = 'White dragon',
            elseif suit == 'f' then
                     [6] = 'Green dragon',
                 local flowerNames = {
                     [7] = 'Red dragon'
                     [1] = 'Plum flower',
                     [2] = 'Orchid flower',
                     [3] = 'Chrysanthemum flower',
                     [4] = 'Bamboo flower',
                     [5] = 'Spring season',
                     [6] = 'Summer season',
                     [7] = 'Autumn season',
                    [8] = 'Winter season'
                 }
                 }
                 label = honors[n]
                 label = flowerNames[n]
             elseif suit == 'f' then
 
                 label = 'Flower or season ' .. n
             elseif n == 0 then
                 label = 'Red five, ' .. suitNames[suit]
 
             else
             else
                 local names = {
                 label = n .. ' of ' .. suitNames[suit]
                    m = 'characters',
                    p = 'circles',
                    s = 'bamboo'
                }
                if n == 0 then
                    label = 'Red five, ' .. names[suit]
                else
                    label = n .. ' of ' .. names[suit]
                end
             end
             end


             local title = mw.title.new('File:' .. filename)
            -- Confirm that the SVG file exists on the wiki.
            local file = title and title.file
             local fileTitle = mw.title.new('File:' .. filename)


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


             local img = mw.html.create('img')
             -- Return actual MediaWiki file-link syntax.
                :attr('src', file:getUrl())
            -- The wiki parser will render this as an image.
                 :attr('alt', label)
            local imageMarkup =
                :attr('title', label)
                 '[[File:' .. filename ..
                 :css('width', tostring(width) .. 'px')
                 '|frameless|' .. tostring(size) ..
                 :css('height', 'auto')
                 'px|alt=' .. label ..
                 :css('vertical-align', 'middle')
                 '|link=]]'


             table.insert(output, tostring(img))
             table.insert(output, imageMarkup)
         end
         end


         position = position + #group + 1
         position = position + #group + 1
     end
     end
     return '<span class="mahjong-tiles">' ..
 
         table.concat(output, '&nbsp;') .. '</span>'
    -- Separate the tiles with non-breaking spaces and keep them inline.
     return '<span class="mahjong-tiles" ' ..
        'style="white-space:normal;">' ..
         table.concat(output, '&nbsp;') ..
        '</span>'
end
end


function p.main(frame)
function p.main(frame)
     local args = frame.args
     local args = frame.args
    -- Use template parameters when invoked through Template:MahjongTile.
     if frame:getParent() then
     if frame:getParent() then
         args = frame:getParent().args
         args = frame:getParent().args
     end
     end
     return render(args)
     return render(args)
end
end


return p
return p

Revisi terkini sejak 12 Oktober 2026 17.33

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

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

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

    -- Ignore whitespace in the notation.
    tiles = tiles:gsub('%s+', '')

    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 output = {}
    local position = 1

    while position <= #tiles do
        local rest = tiles:sub(position)

        -- Read a sequence of digits followed by its suit.
        local group, suit = rest:match('^(%d+)([mpszfx])')

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

        -- Validate and render each tile in the group.
        for i = 1, #group do
            local n = tonumber(group:sub(i, i))
            local valid = false

            if suit == 'x' then
                -- The tile back is represented by 1x only.
                valid = n == 1 and #group == 1

            elseif suit == 'm' or suit == 'p' or suit == 's' then
                -- Numbered suits, including red fives (0).
                valid = (n >= 1 and n <= 9) or n == 0

            elseif suit == 'z' then
                -- Four winds and three dragons.
                valid = n >= 1 and n <= 7

            elseif suit == 'f' then
                -- Four flowers and four seasons.
                valid = n >= 1 and n <= 8
            end

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

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

            if suit == 'x' then
                label = 'Mahjong tile back'

            elseif suit == 'z' then
                label = honorNames[n]

            elseif suit == 'f' then
                local flowerNames = {
                    [1] = 'Plum flower',
                    [2] = 'Orchid flower',
                    [3] = 'Chrysanthemum flower',
                    [4] = 'Bamboo flower',
                    [5] = 'Spring season',
                    [6] = 'Summer season',
                    [7] = 'Autumn season',
                    [8] = 'Winter season'
                }
                label = flowerNames[n]

            elseif n == 0 then
                label = 'Red five, ' .. suitNames[suit]

            else
                label = n .. ' of ' .. suitNames[suit]
            end

            -- Confirm that the SVG file exists on the wiki.
            local fileTitle = mw.title.new('File:' .. filename)

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

            -- Return actual MediaWiki file-link syntax.
            -- The wiki parser will render this as an image.
            local imageMarkup =
                '[[File:' .. filename ..
                '|frameless|' .. tostring(size) ..
                'px|alt=' .. label ..
                '|link=]]'

            table.insert(output, imageMarkup)
        end

        position = position + #group + 1
    end

    -- Separate the tiles with non-breaking spaces and keep them inline.
    return '<span class="mahjong-tiles" ' ..
        'style="white-space:normal;">' ..
        table.concat(output, '&nbsp;') ..
        '</span>'
end

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

    -- Use template parameters when invoked through Template:MahjongTile.
    if frame:getParent() then
        args = frame:getParent().args
    end

    return render(args)
end

return p