Modul:String count

Die Dokumentation für dieses Modul kann unter Modul:String count/Doku erstellt werden

Die Dokumentation für dieses Modul kann unter Modul:String count/Doku erstellt werden

-- This module counts the number of times a string appears on a given page.

local yesno = require('Module:Yesno')

local p = {}

local function escapePattern(s)
	-- Escape punctuation in a string so it can be used in a Lua pattern.
	s = s:gsub('%p', '%%%0')
	return s
end

function p._count(args)
	-- Get the page text, checking for common errors.
	if not args.page then
		error("no 'page' argument; please provide the name of the page you " ..
			"wish to count strings in", 2)
	end
	local title = mw.title.new(args.page)
	if not title then
		error(string.format(
			"'%s' is not a valid page name; check for invalid characters",
			args.page
		), 2)
	end
	local text = title:getContent()
	if not text then
		error(string.format(
			"could not get the content of page '%s'; check that it exists",
			title.prefixedText
		), 2)
	end
	
	-- Get the pattern
	local pattern = args.search
	if not pattern then
		error("no 'search' argument; please provide the string you wish to search for", 2)
	elseif yesno(args.plain) ~= false then
		pattern = escapePattern(pattern)
	end

	-- Find the count
	local temp, count = mw.ustring.gsub(text, pattern, '%0')
	return count
end

function p.count(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:String count'
	})
	return p._count(args)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.