Difference between revisions of "Module:Ruby"
Dinoguy1000 (talk | contribs) m (Protected "Module:Ruby": High traffic page ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))) |
Dinoguy1000 (talk | contribs) (rewrite some) |
||
Line 1: | Line 1: | ||
− | |||
local p = {} | local p = {} | ||
Line 6: | Line 5: | ||
-- implements {{Ruby/split}} | -- implements {{Ruby/split}} | ||
-- | -- | ||
− | local args | + | local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame |
− | + | ||
− | |||
− | |||
− | |||
− | |||
if not args[1] then | if not args[1] then | ||
return -- nothing was passed to the module | return -- nothing was passed to the module | ||
end | end | ||
− | args[1] | + | if not args[1]:find( '<ruby' ) then |
− | |||
− | |||
return args[1] -- if there's no Ruby markup in the input, just return it | return args[1] -- if there's no Ruby markup in the input, just return it | ||
end | end | ||
− | + | local str = mw.text.trim( args[1] ) | |
− | local | + | local el = 'rb' |
− | + | if args[2] and not args[2]:find( 'base' ) then | |
− | + | el = 'rt' | |
− | + | end | |
− | + | ||
− | + | local out = {} | |
− | + | table.insert( out, str:match( '^(.-)<ruby' ) ) -- grab anything before the first <ruby> tag | |
− | + | for piece in mw.text.gsplit( str, '<ruby' ) do | |
+ | table.insert( out, table.concat( { piece:match( ( '<%s[^>]*>(.-)</%s>' ):format( el, el ) ), piece:match( '</ruby>(.-)$' ) } ) ) | ||
end | end | ||
− | return | + | return table.concat( out, '' ) |
end | end | ||
return p | return p | ||
− |
Revision as of 07:14, 12 August 2023
local p = {}
function p.split( frame )
--
-- implements {{Ruby/split}}
--
local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame
if not args[1] then
return -- nothing was passed to the module
end
if not args[1]:find( '<ruby' ) then
return args[1] -- if there's no Ruby markup in the input, just return it
end
local str = mw.text.trim( args[1] )
local el = 'rb'
if args[2] and not args[2]:find( 'base' ) then
el = 'rt'
end
local out = {}
table.insert( out, str:match( '^(.-)<ruby' ) ) -- grab anything before the first <ruby> tag
for piece in mw.text.gsplit( str, '<ruby' ) do
table.insert( out, table.concat( { piece:match( ( '<%s[^>]*>(.-)</%s>' ):format( el, el ) ), piece:match( '</ruby>(.-)$' ) } ) )
end
return table.concat( out, '' )
end
return p