Permanently protected module

Difference between revisions of "Module:Ruby"

From Yugipedia
Jump to: navigation, search
m (Protected "Module:Ruby": High traffic page ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)))
(rewrite some)
Line 1: Line 1:
-- <pre>
 
 
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 frame == mw.getCurrentFrame() then
+
args = frame:getParent().args
 
else
 
args = frame
 
end
 
 
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] = mw.text.trim( args[1] )
+
if not args[1]:find( '<ruby' ) then
 
 
if not mw.ustring.find( args[1], '<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
  
args[2] = args[2] and ( mw.text.trim( args[2] ) == 'base' and '' or mw.text.trim( args[2] ) ) or ''
+
local str = mw.text.trim( args[1] )
  
local first, parts, match
+
local el = 'rb'
first = mw.ustring.match( args[1], '^(.-)<ruby' ) or '' -- grab anything before the first <ruby> tag since otherwise it'll be lost
+
if args[2] and not args[2]:find( 'base' ) then
parts = mw.text.split( args[1], '<ruby' )
+
el = 'rt'
match = args[2] == '' and '<rb[^>]*>(.-)</rb>' or '<rt[^>]*>(.-)</rt>'
+
end
 
+
for i = 1, #parts do
+
local out = {}
parts[i] = ( mw.ustring.match( parts[i], match ) or '' )
+
table.insert( out, str:match( '^(.-)<ruby' ) ) -- grab anything before the first <ruby> tag
.. ( mw.ustring.match( parts[i], '</ruby>(.*)$' ) or '' ) -- grab anything between </ruby> and the next <ruby> (or the end of the string, if this is the last part)
+
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 first, table.concat( parts, '' )
+
return table.concat( out, '' )
 
end
 
end
  
 
return p
 
return p
-- </pre>
 

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