<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.filipefonseca.pt/index.php?action=history&amp;feed=atom&amp;title=Module%3AString2</id>
	<title>Module:String2 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.filipefonseca.pt/index.php?action=history&amp;feed=atom&amp;title=Module%3AString2"/>
	<link rel="alternate" type="text/html" href="http://wiki.filipefonseca.pt/index.php?title=Module:String2&amp;action=history"/>
	<updated>2026-04-19T00:24:26Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.1</generator>
	<entry>
		<id>http://wiki.filipefonseca.pt/index.php?title=Module:String2&amp;diff=4916&amp;oldid=prev</id>
		<title>FilipeFonseca: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="http://wiki.filipefonseca.pt/index.php?title=Module:String2&amp;diff=4916&amp;oldid=prev"/>
		<updated>2021-07-08T14:35:51Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
p.trim = function(frame)&lt;br /&gt;
	return mw.text.trim(frame.args[1] or &amp;quot;&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
p.sentence = function (frame)&lt;br /&gt;
	-- {{lc:}} is strip-marker safe, string.lower is not.&lt;br /&gt;
	frame.args[1] = frame:callParserFunction('lc', frame.args[1])&lt;br /&gt;
	return p.ucfirst(frame)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
p.ucfirst = function (frame )&lt;br /&gt;
	local s =  mw.text.trim( frame.args[1] or &amp;quot;&amp;quot; )&lt;br /&gt;
	local s1 = &amp;quot;&amp;quot;&lt;br /&gt;
	-- if it's a list chop off and (store as s1) everything up to the first &amp;lt;li&amp;gt;&lt;br /&gt;
	local lipos = mw.ustring.find(s, &amp;quot;&amp;lt;li&amp;gt;&amp;quot; )&lt;br /&gt;
	if lipos then&lt;br /&gt;
		s1 = mw.ustring.sub(s, 1, lipos + 3)&lt;br /&gt;
		s = mw.ustring.sub(s, lipos + 4)&lt;br /&gt;
	end&lt;br /&gt;
	-- s1 is either &amp;quot;&amp;quot; or the first part of the list markup, so we can continue&lt;br /&gt;
	-- and prepend s1 to the returned string&lt;br /&gt;
	local letterpos&lt;br /&gt;
	if mw.ustring.find(s, &amp;quot;^%[%[[^|]+|[^%]]+%]%]&amp;quot;) then&lt;br /&gt;
		-- this is a piped wikilink, so we capitalise the text, not the pipe&lt;br /&gt;
		local _&lt;br /&gt;
		_, letterpos = mw.ustring.find(s, &amp;quot;|%A*%a&amp;quot;) -- find the first letter after the pipe&lt;br /&gt;
	else&lt;br /&gt;
		letterpos = mw.ustring.find(s, '%a')&lt;br /&gt;
	end&lt;br /&gt;
	if letterpos then&lt;br /&gt;
		local first = mw.ustring.sub(s, 1, letterpos - 1)&lt;br /&gt;
		local letter = mw.ustring.sub(s, letterpos, letterpos)&lt;br /&gt;
		local rest = mw.ustring.sub(s, letterpos + 1)&lt;br /&gt;
		return s1 .. first .. mw.ustring.upper(letter) .. rest&lt;br /&gt;
	else&lt;br /&gt;
		return s1 .. s&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
p.title = function (frame )&lt;br /&gt;
	-- http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html&lt;br /&gt;
	-- recommended by The U.S. Government Printing Office Style Manual:&lt;br /&gt;
	-- &amp;quot;Capitalize all words in titles of publications and documents,&lt;br /&gt;
	-- except a, an, the, at, by, for, in, of, on, to, up, and, as, but, or, and nor.&amp;quot;&lt;br /&gt;
	local alwayslower = {['a'] = 1, ['an'] = 1, ['the'] = 1,&lt;br /&gt;
		['and'] = 1, ['but'] = 1, ['or'] = 1, ['for'] = 1,&lt;br /&gt;
		['nor'] = 1, ['on'] = 1, ['in'] = 1, ['at'] = 1, ['to'] = 1,&lt;br /&gt;
		['from'] = 1, ['by'] = 1, ['of'] = 1, ['up'] = 1 }&lt;br /&gt;
	local res = ''&lt;br /&gt;
	local s =  mw.text.trim( frame.args[1] or &amp;quot;&amp;quot; )&lt;br /&gt;
	local words = mw.text.split( s, &amp;quot; &amp;quot;)&lt;br /&gt;
	for i, s in ipairs(words) do&lt;br /&gt;
		-- {{lc:}} is strip-marker safe, string.lower is not.&lt;br /&gt;
		s = frame:callParserFunction('lc', s)&lt;br /&gt;
		if i == 1 or alwayslower[s] ~= 1 then&lt;br /&gt;
			s = mw.getContentLanguage():ucfirst(s)&lt;br /&gt;
		end&lt;br /&gt;
		words[i] = s&lt;br /&gt;
	end&lt;br /&gt;
	return table.concat(words, &amp;quot; &amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- findlast finds the last item in a list&lt;br /&gt;
-- the first unnamed parameter is the list&lt;br /&gt;
-- the second, optional unnamed parameter is the list separator (default = comma space)&lt;br /&gt;
-- returns the whole list if separator not found&lt;br /&gt;
p.findlast = function(frame)&lt;br /&gt;
	local s =  mw.text.trim( frame.args[1] or &amp;quot;&amp;quot; )&lt;br /&gt;
	local sep = frame.args[2] or &amp;quot;&amp;quot;&lt;br /&gt;
	if sep == &amp;quot;&amp;quot; then sep = &amp;quot;, &amp;quot; end&lt;br /&gt;
	local pattern = &amp;quot;.*&amp;quot; .. sep .. &amp;quot;(.*)&amp;quot;&lt;br /&gt;
	local a, b, last = s:find(pattern)&lt;br /&gt;
	if a then&lt;br /&gt;
		return last&lt;br /&gt;
	else&lt;br /&gt;
		return s&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- stripZeros finds the first number and strips leading zeros (apart from units)&lt;br /&gt;
-- e.g &amp;quot;0940&amp;quot; -&amp;gt; &amp;quot;940&amp;quot;; &amp;quot;Year: 0023&amp;quot; -&amp;gt; &amp;quot;Year: 23&amp;quot;; &amp;quot;00.12&amp;quot; -&amp;gt; &amp;quot;0.12&amp;quot;&lt;br /&gt;
p.stripZeros = function(frame)&lt;br /&gt;
	local s = mw.text.trim(frame.args[1] or &amp;quot;&amp;quot;)&lt;br /&gt;
	local n = tonumber( string.match( s, &amp;quot;%d+&amp;quot; ) ) or &amp;quot;&amp;quot;&lt;br /&gt;
	s = string.gsub( s, &amp;quot;%d+&amp;quot;, n, 1 )&lt;br /&gt;
	return s&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- nowiki ensures that a string of text is treated by the MediaWiki software as just a string&lt;br /&gt;
-- it takes an unnamed parameter and trims whitespace, then removes any wikicode&lt;br /&gt;
p.nowiki = function(frame)&lt;br /&gt;
	local str = mw.text.trim(frame.args[1] or &amp;quot;&amp;quot;)&lt;br /&gt;
	return mw.text.nowiki(str)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- posnq (position, no quotes) returns the numerical start position of the first occurrence&lt;br /&gt;
-- of one piece of text (&amp;quot;match&amp;quot;) inside another (&amp;quot;str&amp;quot;).&lt;br /&gt;
-- It returns nil if no match is found, or if either parameter is blank.&lt;br /&gt;
-- It takes the text to be searched in as the first unnamed parameter, which is trimmed.&lt;br /&gt;
-- It takes the text to match as the second unnamed parameter, which is trimmed and&lt;br /&gt;
-- any double quotes &amp;quot; are stripped out.&lt;br /&gt;
p.posnq = function(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	local pargs = frame:getParent().args&lt;br /&gt;
	for k, v in pairs(pargs) do&lt;br /&gt;
		args[k] = v&lt;br /&gt;
	end&lt;br /&gt;
	local str = mw.text.trim(args[1] or args.source or &amp;quot;&amp;quot;)&lt;br /&gt;
	local match = mw.text.trim(args[2] or args.target or &amp;quot;&amp;quot;):gsub('&amp;quot;', '')&lt;br /&gt;
	if str == &amp;quot;&amp;quot; or match == &amp;quot;&amp;quot; then return nil end&lt;br /&gt;
	local plain = mw.text.trim(args[3] or args.plain or &amp;quot;&amp;quot;)&lt;br /&gt;
	if plain == &amp;quot;false&amp;quot; then plain = false else plain = true end&lt;br /&gt;
	local nomatch = mw.text.trim(args[4] or args.nomatch or &amp;quot;&amp;quot;)&lt;br /&gt;
	-- just take the start position&lt;br /&gt;
	local pos = mw.ustring.find(str, match, 1, plain) or nomatch&lt;br /&gt;
	return pos&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- split splits text at boundaries specified by separator&lt;br /&gt;
-- and returns the chunk for the index idx (starting at 1)&lt;br /&gt;
-- #invoke:String2 |split |text |separator |index |true/false&lt;br /&gt;
-- #invoke:String2 |split |txt=text |sep=separator |idx=index |plain=true/false&lt;br /&gt;
-- if plain is false/no/0 then separator is treated as a Lua pattern - defaults to plain=true&lt;br /&gt;
p.split = function(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	if not(args[1] or args.txt) then args = frame:getParent().args end&lt;br /&gt;
	local txt = args[1] or args.txt or &amp;quot;&amp;quot;&lt;br /&gt;
	if txt == &amp;quot;&amp;quot; then return nil end&lt;br /&gt;
	local sep = (args[2] or args.sep or &amp;quot;&amp;quot;):gsub('&amp;quot;', '')&lt;br /&gt;
	local idx = tonumber(args[3] or args.idx) or 1&lt;br /&gt;
	local plain = (args[4] or args.plain or &amp;quot;true&amp;quot;):sub(1,1)&lt;br /&gt;
	plain = (plain ~= &amp;quot;f&amp;quot; and plain ~= &amp;quot;n&amp;quot; and plain ~= &amp;quot;0&amp;quot;)&lt;br /&gt;
	local splittbl = mw.text.split( txt, sep, plain )&lt;br /&gt;
	if idx &amp;lt; 0 then idx = #splittbl + idx + 1 end&lt;br /&gt;
	return splittbl[idx]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- val2percent scans through a string, passed as either the first unnamed parameter or |txt=&lt;br /&gt;
-- it converts each number it finds into a percentage and returns the resultant string.&lt;br /&gt;
p.val2percent = function(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	if not(args[1] or args.txt) then args = frame:getParent().args end&lt;br /&gt;
	local txt = mw.text.trim(args[1] or args.txt or &amp;quot;&amp;quot;)&lt;br /&gt;
	if txt == &amp;quot;&amp;quot; then return nil end&lt;br /&gt;
	local function v2p (x)&lt;br /&gt;
		x = (tonumber(x) or 0) * 100&lt;br /&gt;
		if x == math.floor(x) then x = math.floor(x) end&lt;br /&gt;
		return x .. &amp;quot;%&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	txt = txt:gsub(&amp;quot;%d[%d%.]*&amp;quot;, v2p) -- store just the string&lt;br /&gt;
	return txt&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- one2a scans through a string, passed as either the first unnamed parameter or |txt=&lt;br /&gt;
-- it converts each occurrence of 'one ' into either 'a ' or 'an ' and returns the resultant string.&lt;br /&gt;
p.one2a = function(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	if not(args[1] or args.txt) then args = frame:getParent().args end&lt;br /&gt;
	local txt = mw.text.trim(args[1] or args.txt or &amp;quot;&amp;quot;)&lt;br /&gt;
	if txt == &amp;quot;&amp;quot; then return nil end&lt;br /&gt;
	txt = txt:gsub(&amp;quot; one &amp;quot;, &amp;quot; a &amp;quot;):gsub(&amp;quot;^one&amp;quot;, &amp;quot;a&amp;quot;):gsub(&amp;quot;One &amp;quot;, &amp;quot;A &amp;quot;):gsub(&amp;quot;a ([aeiou])&amp;quot;, &amp;quot;an %1&amp;quot;):gsub(&amp;quot;A ([aeiou])&amp;quot;, &amp;quot;An %1&amp;quot;)&lt;br /&gt;
	return txt&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- findpagetext returns the position of a piece of text in a page&lt;br /&gt;
-- First positional parameter or |text is the search text&lt;br /&gt;
-- Optional parameter |title is the page title, defaults to current page&lt;br /&gt;
-- Optional parameter |plain is either true for plain search (default) or false for Lua pattern search&lt;br /&gt;
-- Optional parameter |nomatch is the return value when no match is found; default is nil&lt;br /&gt;
p._findpagetext = function(args)&lt;br /&gt;
	-- process parameters&lt;br /&gt;
	local nomatch = args.nomatch or &amp;quot;&amp;quot;&lt;br /&gt;
	if nomatch == &amp;quot;&amp;quot; then nomatch = nil end&lt;br /&gt;
	--&lt;br /&gt;
	local text = mw.text.trim(args[1] or args.text or &amp;quot;&amp;quot;)&lt;br /&gt;
	if text == &amp;quot;&amp;quot; then return nil end&lt;br /&gt;
	--&lt;br /&gt;
	local title = args.title or &amp;quot;&amp;quot;&lt;br /&gt;
	local titleobj&lt;br /&gt;
	if title == &amp;quot;&amp;quot; then&lt;br /&gt;
		titleobj = mw.title.getCurrentTitle()&lt;br /&gt;
	else&lt;br /&gt;
		titleobj = mw.title.new(title)&lt;br /&gt;
	end&lt;br /&gt;
	--&lt;br /&gt;
	local plain = args.plain or &amp;quot;&amp;quot;&lt;br /&gt;
	if plain:sub(1, 1) == &amp;quot;f&amp;quot; then plain = false else plain = true end&lt;br /&gt;
	-- get the page content and look for 'text' - return position or nomatch&lt;br /&gt;
	local content = titleobj:getContent()&lt;br /&gt;
	return mw.ustring.find(content, text, 1, plain) or nomatch	-- returns multiple values&lt;br /&gt;
end&lt;br /&gt;
p.findpagetext = function(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	local pargs = frame:getParent().args&lt;br /&gt;
	for k, v in pairs(pargs) do&lt;br /&gt;
		args[k] = v&lt;br /&gt;
	end&lt;br /&gt;
	if not (args[1] or args.text) then return nil end&lt;br /&gt;
	-- just the first value&lt;br /&gt;
	return (p._findpagetext(args))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- returns the decoded url. Inverse of parser function {{urlencode:val|TYPE}}&lt;br /&gt;
-- Type is:&lt;br /&gt;
-- QUERY decodes + to space (default)&lt;br /&gt;
-- PATH does no extra decoding&lt;br /&gt;
-- WIKI decodes _ to space&lt;br /&gt;
p._urldecode = function(url, type)&lt;br /&gt;
	url = url or &amp;quot;&amp;quot;&lt;br /&gt;
	type = (type == &amp;quot;PATH&amp;quot; or type == &amp;quot;WIKI&amp;quot;) and type&lt;br /&gt;
	return mw.uri.decode( url, type )&lt;br /&gt;
end&lt;br /&gt;
-- {{#invoke:String2|urldecode|url=url|type=type}}&lt;br /&gt;
p.urldecode = function(frame)&lt;br /&gt;
	return mw.uri.decode( frame.args.url, frame.args.type )&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- what follows was merged from Module:StringFunc&lt;br /&gt;
&lt;br /&gt;
-- helper functions&lt;br /&gt;
p._GetParameters = require('Module:GetParameters')&lt;br /&gt;
&lt;br /&gt;
-- Argument list helper function, as per Module:String&lt;br /&gt;
p._getParameters = p._GetParameters.getParameters&lt;br /&gt;
&lt;br /&gt;
-- Escape Pattern helper function so that all characters are treated as plain text, as per Module:String&lt;br /&gt;
function p._escapePattern( pattern_str)&lt;br /&gt;
	return mw.ustring.gsub( pattern_str, &amp;quot;([%(%)%.%%%+%-%*%?%[%^%$%]])&amp;quot;, &amp;quot;%%%1&amp;quot; );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Helper Function to interpret boolean strings, as per Module:String&lt;br /&gt;
p._getBoolean = p._GetParameters.getBoolean&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Strip&lt;br /&gt;
&lt;br /&gt;
This function Strips characters from string&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
{{#invoke:String2|strip|source_string|characters_to_strip|plain_flag}}&lt;br /&gt;
&lt;br /&gt;
Parameters&lt;br /&gt;
	source: The string to strip&lt;br /&gt;
	chars:  The pattern or list of characters to strip from string, replaced with ''&lt;br /&gt;
	plain:  A flag indicating that the chars should be understood as plain text. defaults to true.&lt;br /&gt;
&lt;br /&gt;
Leading and trailing whitespace is also automatically stripped from the string.&lt;br /&gt;
]]&lt;br /&gt;
function p.strip( frame )&lt;br /&gt;
	local new_args = p._getParameters( frame.args,  {'source', 'chars', 'plain'} )&lt;br /&gt;
	local source_str = new_args['source'] or '';&lt;br /&gt;
	local chars = new_args['chars'] or '' or 'characters';&lt;br /&gt;
	source_str = mw.text.trim(source_str);&lt;br /&gt;
	if source_str == '' or chars == '' then&lt;br /&gt;
		return source_str;&lt;br /&gt;
	end&lt;br /&gt;
	local l_plain = p._getBoolean( new_args['plain'] or true );&lt;br /&gt;
	if l_plain then&lt;br /&gt;
		chars = p._escapePattern( chars );&lt;br /&gt;
	end&lt;br /&gt;
	local result;&lt;br /&gt;
	result = mw.ustring.gsub(source_str, &amp;quot;[&amp;quot;..chars..&amp;quot;]&amp;quot;, '')&lt;br /&gt;
	return result;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Match any&lt;br /&gt;
Returns the index of the first given pattern to match the input. Patterns must be consecutively numbered.&lt;br /&gt;
Returns the empty string if nothing matches for use in {{#if:}}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
	{{#invoke:String2|matchAll|source=123 abc|456|abc}} returns '2'.&lt;br /&gt;
&lt;br /&gt;
Parameters:&lt;br /&gt;
	source: the string to search&lt;br /&gt;
	plain:  A flag indicating that the patterns should be understood as plain text. defaults to true.&lt;br /&gt;
	1, 2, 3, ...: the patterns to search for&lt;br /&gt;
]]&lt;br /&gt;
function p.matchAny(frame)&lt;br /&gt;
	local source_str = frame.args['source'] or error('The source parameter is mandatory.')&lt;br /&gt;
	local l_plain = p._getBoolean( frame.args['plain'] or true )&lt;br /&gt;
	for i = 1, math.huge do&lt;br /&gt;
		local pattern = frame.args[i]&lt;br /&gt;
		if not pattern then return '' end&lt;br /&gt;
		if mw.ustring.find(source_str, pattern, 1, l_plain) then&lt;br /&gt;
			return tostring(i)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>FilipeFonseca</name></author>
	</entry>
</feed>