Module:Utilities: Difference between revisions

From devsummit
(Created page with "local p = {} function p.substring(frame) s = frame.args[1] if ( #s < 100 ) then return s else return string.sub( s .. '...', 1, 100 ) end end function p.process_para...")
 
No edit summary
Line 6: Line 6:
return s
return s
else
else
return string.sub( s .. '...', 1, 100 )
return string.sub( s, 1, 100 ) .. '...'
end
end
end
end

Revision as of 16:39, 13 November 2017

Documentation for this module may be created at Module:Utilities/doc

local p = {}

function p.substring(frame)
	s = frame.args[1]
	if ( #s < 100 ) then
		return s
	else
		return string.sub( s, 1, 100 ) .. '...'
	end
end

function p.process_param(param)

	s = mw.text.trim( param )

	s = string.gsub( s, '%s*,%s*', ',' )
	s = string.gsub( s, ',+', ',' )
	if ( string.match( s, ',$' ) ) then
		s = string.sub( s, 1, #s - 1 )
	end
	if ( string.find( s, ',' ) == 1 ) then
		length = 0 - #s + 1
		s = string.sub( s, length )
	end
	if #s == 0 then
		return ''
	end

	return mw.text.split( s, ",", true )
end

function p.parseMultiple(frame)

	items = p.process_param(frame.args[1])

	table.sort( items, lowercompare )

	links = {}
	for k,v in pairs(items) do
		links[k] = frame:expandTemplate{ title = frame.args[2], args = { v } }
	end

	return table.concat( links, frame.args[3] );
end

function lowercompare(a,b)
	return string.lower(a) < string.lower(b)
end

return p