Difference between revisions of "Module:Common"

From Foundation - Wiki
Jump to navigation Jump to search
 
Line 33: Line 33:
local disambig = string.find(title, '%(')
local disambig = string.find(title, '%(')
if disambig then
if disambig then
title = title:gsub('(.*)%(.-%)', '%1')
title = title:gsub('(.*)%s%(.-%)', '%1')
end
end

Latest revision as of 18:59, 7 February 2024

Additional functions that are common to other modules. Although they could be called into templates the overhead call into lua would generally greater than using a built in mediawiki parser function where one exists.

When calling in to another module, use local com = require( "Module:Common"). This will ensure consistency between modules.


local a = require('Module:Arguments')
local com = {}

function com.trim(object)
	--Use of while instead of if incase multiple whitespaces are present.
	if com.exists(object) then
		while string.sub(object, 1, 1) == ' ' do 
			object = string.sub(object, 2)
		end
	
		while string.sub(object, string.len(object)) == ' ' do
			object = string.sub(object, 1, string.len(object) - 1)
		end
	end
	
	return object
end

function com.exists(object)
	if object == nil or object == '' then
		return false
	else
		return true
	end
end

function com.modulePagename(title)
	
	if not title then
		title = mw.title.getCurrentTitle().text
	end
	mw.log(title)
	local disambig = string.find(title, '%(')
	if disambig then
		title = title:gsub('(.*)%s%(.-%)', '%1')
	end
	
	return title
end

function com.pagename(frame)
	local args = a.getArgs(frame)
	return com.trim(com.modulePagename(args['title']))
end

function com.fUpper(text)
	local words = mw.text.split(text, '%s')
	for k, v in ipairs(words) do
		words[k] = v:sub(1,1):upper() .. (v:sub(2) or '')
	end
	
	return table.concat(words, ' ')
end

function com.firstUpper(frame)
	local args = a.getArgs(frame)
	return string.gsub(args[1], string.sub(args[1],1,1), string.upper(
		string.sub(args[1],1,1)))
end

return com