Modul:MahjongTile: Perbedaan antara revisi
Tidak ada ringkasan suntingan |
Tidak ada ringkasan suntingan |
||
| 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 = | local size = tonumber(args.size or '32') | ||
local variant = mw.text.trim(args.variant or 'riichi') | local variant = mw.text.trim(args.variant or 'riichi') | ||
:lower() | :lower() | ||
| Baris 21: | Baris 21: | ||
end | end | ||
if not size or size < 12 or size > 120 then | |||
if not | return errorMessage('size must be between 12 and 120 pixels.') | ||
return errorMessage('size must be | |||
end | end | ||
-- Remove whitespace between groups. | |||
tiles = tiles:gsub('%s+', '') | tiles = tiles:gsub('%s+', '') | ||
local output = {} | local output = {} | ||
local position = 1 | 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 | while position <= #tiles do | ||
| Baris 36: | Baris 52: | ||
if not group then | if not group then | ||
return errorMessage('Invalid notation near "' .. | return errorMessage( | ||
'Invalid notation near "' .. rest .. '".' | |||
) | |||
end | end | ||
| Baris 53: | Baris 70: | ||
if not valid then | if not valid then | ||
return errorMessage('Invalid tile number ' .. | return errorMessage( | ||
'Invalid tile number ' .. n .. | |||
' for suit ' .. suit .. '.' | |||
) | |||
end | end | ||
if suit == 'f' and variant == 'riichi' then | if suit == 'f' and variant == 'riichi' then | ||
return errorMessage( | return errorMessage( | ||
'Flower tiles require variant=mcr.') | 'Flower tiles require variant=mcr.' | ||
) | |||
end | end | ||
| Baris 66: | Baris 86: | ||
if suit == 'z' then | if suit == 'z' then | ||
label = honorNames[n] | |||
label = | |||
elseif suit == 'f' then | elseif suit == 'f' then | ||
label = 'Flower or season ' .. n | label = 'Flower or season ' .. n | ||
elseif n == 0 then | |||
label = 'Red five, ' .. suitNames[suit] | |||
else | else | ||
label = n .. ' of ' .. suitNames[suit] | |||
end | 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 | end | ||
position = position + #group + 1 | position = position + #group + 1 | ||
end | 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 | end | ||
Revisi per 12 Oktober 2026 17.20
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