Modul:MahjongTile: Perbedaan antara revisi
Tidak ada ringkasan suntingan |
Tidak ada ringkasan suntingan |
||
| (6 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 = | local size = tonumber(args.size or '32') | ||
if tiles == '' then | if tiles == '' then | ||
| Baris 17: | Baris 15: | ||
end | end | ||
if | if not size or size < 12 or size > 120 then | ||
return errorMessage(' | return errorMessage('size must be between 12 and 120 pixels.') | ||
end | 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 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+)([ | |||
-- 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( | ||
'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( | return errorMessage( | ||
' | '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 | label = 'Mahjong tile back' | ||
[1] = ' | |||
[2] = ' | elseif suit == 'z' then | ||
[3] = ' | label = honorNames[n] | ||
[4] = ' | |||
[5] = ' | elseif suit == 'f' then | ||
[6] = ' | local flowerNames = { | ||
[7] = ' | [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 = | label = flowerNames[n] | ||
elseif | |||
label = ' | elseif n == 0 then | ||
label = 'Red five, ' .. suitNames[suit] | |||
else | 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 | 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 | end | ||
position = position + #group + 1 | position = position + #group + 1 | ||
end | end | ||
return '<span class="mahjong-tiles">' .. | |||
table.concat(output, ' ') .. '</span>' | -- 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 | 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, ' ') ..
'</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