Modul:Link utilities: Unterschied zwischen den Versionen
(code check) |
(+find, vereinfacht) |
||
| Zeile 2: | Zeile 2: | ||
local LinkBase = { | local LinkBase = { | ||
suite = 'LinkBase', | suite = 'LinkBase', | ||
serial = '2020- | serial = '2020-08-06', | ||
item = 65228027 | item = 65228027 | ||
} | } | ||
| Zeile 20: | Zeile 20: | ||
-- substitude delimiters | -- substitude delimiters | ||
for | for _, delimiter in ipairs( delimiters ) do | ||
s = mw.ustring.gsub( s, | s = mw.ustring.gsub( s, delimiter, '\0' .. delimiter .. '\0' ); | ||
-- remove zero marks from inside parentheses () | -- remove zero marks from inside parentheses () | ||
s = mw.ustring.gsub( s, '%b()', function( t ) return | s = mw.ustring.gsub( s, '%b()', function( t ) return t:gsub( '%z', '' ) end ) | ||
-- replace delimeters by the default delimiter | -- replace delimeters by the default delimiter | ||
s = mw.ustring.gsub( s, '\0' .. | s = mw.ustring.gsub( s, '\0' .. delimiter .. '\0', '\0' .. defaultDelimiter .. '\0' ); | ||
end | end | ||
| Zeile 43: | Zeile 43: | ||
function lb.extractComment( s ) | function lb.extractComment( s ) | ||
local comment = '' | local comment = '' | ||
local t = | if s:find( '(', 1, true ) then | ||
local t = s:gsub( '^.*(%b())$', '%1' ) | |||
if t ~= s then | |||
comment = t | |||
s = mw.text.trim( s:gsub( '%b()$', '' ) ) | |||
end | |||
end | end | ||
return s, comment | return s, comment | ||
| Zeile 56: | Zeile 58: | ||
aClass = aClass or 'error' | aClass = aClass or 'error' | ||
styles = styles or '' | styles = styles or '' | ||
return catPrefix .. aCat .. ']]' | |||
.. '<span class="' .. aClass .. '" title="' .. aCat .. '"' | |||
.. ( styles ~= '' and ( ' style="' .. styles .. '"' ) or '' ) | |||
.. '>' .. aCat .. '</span>' | |||
end | end | ||
Version vom 6. August 2020, 06:21 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Link utilities/doc erstellt werden
-- documentation
local LinkBase = {
suite = 'LinkBase',
serial = '2020-08-06',
item = 65228027
}
-- module variable
local lb = {}
-- module import
local fs = require( 'Module:Failsafe' )
-- split separate items like numbers
function lb.splitItems( s, delimiters, defaultDelimiter )
defaultDelimiter = defaultDelimiter or ','
-- wrap delimiters with zero marks
s = mw.ustring.gsub( s, defaultDelimiter, '\0' .. defaultDelimiter .. '\0' );
-- substitude delimiters
for _, delimiter in ipairs( delimiters ) do
s = mw.ustring.gsub( s, delimiter, '\0' .. delimiter .. '\0' );
-- remove zero marks from inside parentheses ()
s = mw.ustring.gsub( s, '%b()', function( t ) return t:gsub( '%z', '' ) end )
-- replace delimeters by the default delimiter
s = mw.ustring.gsub( s, '\0' .. delimiter .. '\0', '\0' .. defaultDelimiter .. '\0' );
end
-- results to an array
s = mw.text.split( s, '\0' .. defaultDelimiter .. '\0' )
for i = #s, 1, -1 do
s[ i ] = mw.text.trim( s[ i ] )
if s[ i ] == '' then
table.remove( s, i )
end
end
return s
end
-- extract comment written in parentheses
-- remove spaces between value like phone numbers and comment
function lb.extractComment( s )
local comment = ''
if s:find( '(', 1, true ) then
local t = s:gsub( '^.*(%b())$', '%1' )
if t ~= s then
comment = t
s = mw.text.trim( s:gsub( '%b()$', '' ) )
end
end
return s, comment
end
-- add error category and hint
-- catPrefix contains opening squared brackets
function lb.errorInfo( catPrefix, aCat, aClass, styles )
aClass = aClass or 'error'
styles = styles or ''
return catPrefix .. aCat .. ']]'
.. '<span class="' .. aClass .. '" title="' .. aCat .. '"'
.. ( styles ~= '' and ( ' style="' .. styles .. '"' ) or '' )
.. '>' .. aCat .. '</span>'
end
-- module administration
function lb.LinkBase()
return LinkBase
end
function lb.failsafe( version )
return fs._failsafe( version, LinkBase ) or ''
end
return lb