Permanently protected module

Difference between revisions of "Module:Ruby"

From Yugipedia
Jump to: navigation, search
m (Make "args" a local variable. Convert indentation to tabs. Wrap in <pre> tags, to avoid template expansion.)
m (Protected "Module:Ruby": High traffic page ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)))
(2 intermediate revisions by the same user not shown)
Line 13: Line 13:
 
end
 
end
 
if not args[1] then
 
if not args[1] then
return
+
return -- nothing was passed to the module
 
end
 
end
  
Line 19: Line 19:
  
 
if not mw.ustring.find( args[1], '<ruby' ) then
 
if not mw.ustring.find( args[1], '<ruby' ) then
return args[1]
+
return args[1] -- if there's no Ruby markup in the input, just return it
 
end
 
end
  
Line 25: Line 25:
  
 
local first, parts, match
 
local first, parts, match
first = mw.ustring.match( args[1], '^(.-)<ruby' ) or ''
+
first = mw.ustring.match( args[1], '^(.-)<ruby' ) or '' -- grab anything before the first <ruby> tag since otherwise it'll be lost
 
parts = mw.text.split( args[1], '<ruby' )
 
parts = mw.text.split( args[1], '<ruby' )
 
match = args[2] == '' and '<rb[^>]*>(.-)</rb>' or '<rt[^>]*>(.-)</rt>'
 
match = args[2] == '' and '<rb[^>]*>(.-)</rb>' or '<rt[^>]*>(.-)</rt>'
Line 31: Line 31:
 
for i = 1, #parts do
 
for i = 1, #parts do
 
parts[i] = ( mw.ustring.match( parts[i], match ) or '' )
 
parts[i] = ( mw.ustring.match( parts[i], match ) or '' )
.. ( mw.ustring.match( parts[i], '</ruby>(.*)$' ) or '' )
+
.. ( 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)
 
end
 
end
  

Revision as of 01:38, 13 June 2019

-- <pre>
local p = {}

function p.split( frame )
	--
	-- implements {{Ruby/split}}
	--
	local args
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
	else
		args = frame
	end
	if not args[1] then
		return -- nothing was passed to the module
	end

	args[1] = mw.text.trim( args[1] )

	if not mw.ustring.find( args[1], '<ruby' ) then
		return args[1] -- if there's no Ruby markup in the input, just return it
	end

	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 '' -- grab anything before the first <ruby> tag since otherwise it'll be lost
	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 '' ) -- grab anything between </ruby> and the next <ruby> (or the end of the string, if this is the last part)
	end

	return first, table.concat( parts, '' )
end

return p
-- </pre>