Difference between revisions of "Module:Common"

From Foundation - Wiki
Jump to navigation Jump to search
Line 25: Line 25:
end
end


function com.pagename(frame)
function com.modulePagename(title)
local args = a.getArgs(frame)
local title = args['title']
if not title then
if not title then
title = mw.title.getCurrentTitle()
title = mw.title.getCurrentTitle()
Line 33: Line 31:
local disambig = string.find(title, '%(')
local disambig = string.find(title, '%(')
local check = disambig
if string.sub(title, string.length(title)) == '%)' then
while check do
local check = disambig
local check = string.find(title, '%(', check + 1)
while check do
if check ~= 0 then
local check = string.find(title, '%(', check + 1)
disambig = check
if check ~= 0 then
disambig = check
end
end
end
end
end
title = string.sub(title, 1, check)
title = string.sub(title, 1, disambig)
return title
return title
end
function com.pagename(frame)
local args = a.getArgs(frame)
return com.modulePagename(args['title'])
end
end


return com
return com

Revision as of 16:56, 11 August 2022

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()
	end
	
	local disambig = string.find(title, '%(')
	if string.sub(title, string.length(title)) == '%)' then
		local check = disambig
		while check do
			local check = string.find(title, '%(', check + 1)
			if check ~= 0 then
				disambig = check
			end
		end
	end
	
	title = string.sub(title, 1, disambig)
	return title
end

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

return com