Module:Utilities

From devsummit
Revision as of 18:53, 13 November 2017 by Ccicalese (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

function p.truncate(frame)
	s = frame.args[1]
	if ( #s < 100 ) then
		return s
	else
		return string.sub( s, 1, 200 ) .. '...'
	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