Modul:Article

Fra MotorWiki
Hopp til navigering Hopp til søk

Dokumentasjon for denne modulen kan opprettes på Modul:Article/dok

local scrubs
scrubs = {
	["scrub-tags"] = function(text)
		return mw.ustring.gsub(text, '%b<>', '' )
	end,
	["scrub-templates"] = function(text)
		return mw.ustring.gsub(text, '%b{}', '' )
	end,
	["scrub-extlinks"] = function(text)
		return mw.ustring.gsub(text, '%[[htps]*:?//%w[^%s]+%s*([^%]]*)%]', '%1' )
	end,
	["scrub-magics"] = function(text)
		return mw.ustring.gsub(text, '__[_%w]+__', '' )
	end,
	["scrub-links"] = function(text)
		local text = mw.ustring.gsub(text, '%[%[[^|%]]+%|([^%]]+)%]%]', '%1' )
		text = mw.ustring.gsub(text, '%[%[([^|%]]+)%]%]', '%1' )
		return text
	end,
	["scrub-sections"] = function(text)
		return mw.ustring.gsub(text, '==+[^=\n]+==+', '' )
	end,
	["scrub-all"] = function(text)
		text = scrubs["scrub-tags"](text)
		text = scrubs["scrub-templates"](text)
		text = scrubs["scrub-links"](text)
		text = scrubs["scrub-extlinks"](text)
		text = scrubs["scrub-magics"](text)
		text = scrubs["scrub-sections"](text)
		return text
	end
}

local tests
tests = {
	["length"] = function(text)
		return mw.ustring.len( text )
	end,
	["extlinks"] = function(text)
		local count = 0
		for line in string.gmatch(text, '%[[htps]*:?//%w[^%s]+%s*([^%]]*)%]') do
			count = 1 + count
		end
		return count
	end,
	["links"] = function(text)
		mw.log('enter links')
		local count = 0
		for line in string.gmatch(text, '%[%[[^|%]]+%|[^%]]+%]%]') do
			count = 1 + count
		end
		for line in string.gmatch(text, '%[%[[^|%]]+%]%]') do
			count = 1 + count
		end
		return count
	end,
	["lines"] = function(text)
		local count = 0
		for line in string.gmatch(text, '([^\n]+)') do
			if mw.ustring.find( line, '%w', init, plain ) then
				count = 1 + count
			end
		end
		return count
	end,
	["words"] = function(text)
		local count = 0
		for line in string.gmatch(text, '([^%s]+)') do
			if mw.ustring.find( line, '%a', init, plain ) then
				count = 1 + count
			end
		end
		return count
	end,
	-- anslag
	["characters"] = function(text)
		local _,count = mw.ustring.gsub(text, '%s', '')
		return mw.ustring.len( text ) - count
	end
}

local p = {}
p.tests=tests
p.scrubs = scrubs

function p._stats(content, args)
	for k,v in ipairs(args) do
		local num = tonumber(k)
		if num and num ~= 1 then
			if scrubs[v] then
				content = scrubs[v](content)
			end
		end
	end
	local acc = 0
	for k,v in pairs(args) do
		local num = tonumber(k)
		if not num and k then
			if tests[k] then
				acc = acc + tonumber(v) * tests[k](content)
			end
		end
	end
	return acc
end

function p.stats(frame)
	return p._stats(mw.title.getCurrentTitle():getContent(), (frame or mw.getCurrentFrame()).args)
end

return p