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
-- Remove whitespace between groups.
tiles = tiles:gsub('%s+', '')
local output = {}
local position = 1
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'
}
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)
local imageNode = mw.html.create('span')
:addClass('mahjong-tile')
:css('display', 'inline-block')
:css('vertical-align', 'middle')
:css('line-height', '0')
:css('margin-right', '2px')
if fileTitle and fileTitle.exists then
local image = mw.html.create('img')
:attr('src', fileTitle:localUrl())
:attr('alt', label)
:attr('title', label)
:css('width', tostring(size) .. 'px')
:css('height', 'auto')
:css('max-width', '100%')
imageNode:node(image)
else
imageNode
:css('line-height', 'normal')
:css('font-size', '12px')
:wikitext('[' .. filename .. ']')
end
table.insert(output, tostring(imageNode))
end
position = position + #group + 1
end
local container = mw.html.create('span')
:addClass('mahjong-tiles')
:css('white-space', 'normal')
for i, tile in ipairs(output) do
if i > 1 then
container:wikitext(' ')
end
container:wikitext(tile)
end
return tostring(container)
end
function p.main(frame)
local args = frame.args
if frame:getParent() then
args = frame:getParent().args
end
return render(args)
end
return p