Module:Utilities

From mwstake
Jump to: navigation, search

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

local p = {}

function p.unstrip(frame)
	return mw.text.unstrip(frame.args[1])
end

function p.fixtitle(frame)
	return (string.gsub(frame.args[1], "_", " "))
end

function p.datetime_utc(frame)
	return p.datetime(frame, false)
end

function p.datetime_local(frame)
	return p.datetime(frame, true)
end

function p.datetime(frame, notutc)

	format = "j xg Y"

	date = nil

	hour = nil

	minute = nil

	for k,v in pairs(frame.args) do
		if (k == 1 and v ~= nil and #v > 0) then
			date = v
		elseif (k == 2 and v ~= nil and #v > 0) then
			hour = v
		elseif (k == 3 and v ~= nil and #v > 0) then
			minute = v
		end
	end

	if (date == nil) then
		return ""
	end

	if (string.find(date,":")) then

		format = format .. " H:i (T)"

	elseif (hour ~= nil or minute ~= nil) then

		format = format .. " H:i (T)"

		if (hour == nil) then
			hour = "00"
		end

		if (minute == nil) then
			minute = "00"
		end

		date = date .. " " .. hour .. ":" .. minute

	end

	return mw.getContentLanguage():formatDate(format, date, notutc)
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