Dokumentasi untuk modul ini dapat dibuat di Modul:Mahjong/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, ' ') ..
'</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