Difference between pages "Module:Ruby" and "Module:Ruby/sandbox"

(Difference between pages)
Jump to: navigation, search
Page 1
Page 2
(avoid error if args isn't even an array)
 
(non-greedy matches should do it)
 
Line 2: Line 2:
  
 
function p.split( frame )
 
function p.split( frame )
--
+
    --
-- implements {{Ruby/split}}
+
    -- implements {{Ruby/split}}
--
+
    --
local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame
 
 
if not args or not args[1] then
 
return -- nothing was passed to the module
 
end
 
  
if not args[1]:find( '<ruby' ) then
+
    if frame == mw.getCurrentFrame() then
return args[1] -- if there's no Ruby markup in the input, just return it
+
        args = frame:getParent().args
end
+
    else
 +
        args = frame
 +
    end
 +
    if not args[1] then
 +
        return
 +
    end
  
local str = mw.text.trim( args[1] )
+
    args[1] = mw.text.trim( args[1] )
  
local el = 'rb'
+
    if not mw.ustring.find( args[1], '<ruby' ) then
if args[2] and not args[2]:find( 'base' ) then
+
        return args[1]
el = 'rt'
+
    end
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, '' )
+
    args[2] = args[2] and ( mw.text.trim( args[2] ) == 'base' and '' or mw.text.trim( args[2] ) ) or ''
 +
 
 +
    local first, parts, match
 +
    first = mw.ustring.match( args[1], '^(.-)<ruby' ) or ''
 +
    parts = mw.text.split( args[1], '<ruby' )
 +
    match = args[2] == '' and '<rb[^>]*>(.-)</rb>' or '<rt[^>]*>(.-)</rt>'
 +
 
 +
    for i = 1, #parts do
 +
        parts[i] = ( mw.ustring.match( parts[i], match ) or '' )
 +
                .. ( mw.ustring.match( parts[i], '</ruby>(.*)$' ) or '' )
 +
    end
 +
 
 +
    return first, table.concat( parts, '' )
 
end
 
end
  
 
return p
 
return p