Moduł:utils
Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:utils/opis
local p = {}
local encodeTables = {
PWN = {
[ 'ą' ] = '%B1',
[ 'ć' ] = '%E6',
[ 'ę' ] = '%EA',
[ 'ł' ] = '%B3',
[ 'ń' ] = '%F1',
[ 'ó' ] = '%F3',
[ 'ś' ] = '%B6',
[ 'ź' ] = '%BC',
[ 'ż' ] = '%BF',
[ ' ' ] = '+'
},
diccionari_cat = {
[ 'é' ] = '%E9', [ 'è' ] = '%E8',
[ 'ó' ] = '%F3', [ 'ò' ] = '%F2',
[ 'ç' ] = '%E7',
[ 'ü' ] = '%FC',
[ '·' ] = '%B7',
[ ' ' ] = '+',
}
}
function p.encodeUri( frame )
local text = frame.args[ 1 ] or ''
local data = encodeTables[ frame.args[ 2 ] ]
if not data then return text end
local output = {}
for codepoint in mw.ustring.gcodepoint( text ) do
local char = mw.ustring.char( codepoint )
table.insert( output, data[ char ] or char )
end
return table.concat( output )
end
function p.trimString( frame )
local text = frame.args[ 1 ] or ''
local pos = tonumber( frame.args[ 2 ] or '' )
local pos2 = tonumber( frame.args[ 3 ] or '' )
assert( type( pos ) == 'number', 'drugi parametr nie jest liczbą' )
return mw.ustring.sub( text, pos, pos2 or -1 )
end
function p.indexOf( frame )
local text = frame.args[ 1 ] or ''
local sub = frame.args[ 2 ] or ''
return mw.ustring.find( text, sub, 1, true ) or 0
end
function p.stringLength( frame )
local text = frame.args[ 1 ] or ''
local bytes = frame.args[ 2 ] == 'B'
if bytes then
return string.len( text )
else
return mw.ustring.len( text )
end
end
function p.countSubstring( mainString, subString )
local count, offset, _ = -1, 0
local find
if mw.ustring.len( subString ) == string.len( subString ) then
find = string.find
else
find = mw.ustring.find
end
repeat
_, offset = find( mainString, subString, offset + 1, true )
count = count + 1
until offset == nil
return count
end
function p.countSubstringInPage( subString, pageName )
local title = mw.title.makeTitle( '', pageName )
return title and p.countSubstring( title:getContent(), subString ) or ''
end
function p.countLinksInPage( frame )
local pageName = frame.args[ 1 ] or ''
return p.countSubstringInPage( '[[', pageName )
end
function p.countSectionsInPage( frame )
local pageName = frame.args[ 1 ] or ''
return p.countSubstringInPage( '\n=', pageName )
end
function p.countOrderedItemsInPage( frame )
local pageName = frame.args[ 1 ] or ''
return p.countSubstringInPage( '\#', pageName )
end
function p.countUnorderedItemsInPage( frame )
local pageName = frame.args[ 1 ] or ''
return p.countSubstringInPage( '*', pageName )
end
function p.countTableRowsInPage( frame )
local pageName = frame.args[ 1 ] or ''
return p.countSubstringInPage( '|-', pageName )
end
return p