Modul:Link utilities: Unterschied zwischen den Versionen

Aus skandinavien-wiki.net
K (+styles)
K (19 Versionen von wikivoyage:Modul:Link_utilities importiert)
 
(13 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
-- documentation
-- module variable and administration
local LinkBase = {
local lu = {
suite  = 'LinkBase',
moduleInterface = {
serial = '2019-07-10',
suite  = 'Link utilities',
item  = 65228027
serial = '2022-10-22',
item  = 65228027
}
}
}
-- module variable
local lb = {}


-- module import
-- module import
local fs = require( 'Module:Failsafe' )
-- require( 'strict' )


-- split separate items like numbers
-- split separate items like numbers
function lb.splitItems( s, delimiters, defaultDelimiter )
function lu.splitItems( s, delimiters, defaultDelimiter )
local i
defaultDelimiter = defaultDelimiter or ','
defaultDelimiter = defaultDelimiter or ','


-- wrap delimiters with zero marks
-- wrap delimiters with zero marks
s = mw.ustring.gsub( s, defaultDelimiter, '\0' .. defaultDelimiter .. '\0' );
s = mw.ustring.gsub( s, defaultDelimiter, '\0%1\0' );


-- substitude delimiters
-- substitude delimiters
for i = 1, #delimiters, 1 do
for i, delimiter in ipairs( delimiters ) do
s = mw.ustring.gsub( s, delimiters[i], '\0' .. delimiters[i] .. '\0' );
s = mw.ustring.gsub( s, delimiter, '\0%1\0' );
-- remove zero marks from inside parentheses ()
-- remove zero marks from inside parentheses ()
s = mw.ustring.gsub( s, '%b()', function( t ) return mw.ustring.gsub( t, '%z', '' ) end )
s = s:gsub( '%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' .. delimiters[i] .. '\0', '\0' .. defaultDelimiter .. '\0' );
s = mw.ustring.gsub( s, '\0' .. delimiter .. '\0', '\0' .. defaultDelimiter .. '\0' );
end
end


Zeile 42: Zeile 40:
-- extract comment written in parentheses
-- extract comment written in parentheses
-- remove spaces between value like phone numbers and comment
-- remove spaces between value like phone numbers and comment
function lb.extractComment( s )
function lu.extractComment( s )
local comment = ''
local comment = ''
local t = mw.ustring.gsub( s, '(.*)(%(.*%))$', '%2' )
if s:find( '(', 1, true ) then
if t ~= s then
local t = s:gsub( '^.*(%b())$', '%1' )
comment = t
if t ~= s then
s = mw.ustring.gsub( mw.ustring.gsub( s, '(.*)(%(.*%))$', '%1' ), '( +)$', '' )
comment = t
s = s:gsub( '[%s%c]*%b()$', '' )
end
end
end
return s, comment
return s, comment
end
end


-- add error category and hint
return lu
-- catPrefix contains opening squared brackets
function lb.errorInfo( catPrefix, aCat, aClass, styles )
aClass = aClass or 'error'
styles = styles or ''
local info = catPrefix .. aCat .. ']]' .. '<span class="' .. aClass
.. '" title="' .. aCat .. '"'
if styles ~= '' then
info = info .. ' style="' .. styles .. '"'
end
return info .. '>' .. aCat .. '</span>'
end
 
-- module administration
function lb.LinkBase()
return LinkBase
end
 
function lb.failsafe( version )
return fs._failsafe( version, LinkBase ) or ''
end
 
return lb

Aktuelle Version vom 11. Februar 2023, 20:58 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Link utilities/doc erstellt werden

-- module variable and administration
local lu = {
	moduleInterface = {
		suite  = 'Link utilities',
		serial = '2022-10-22',
		item   = 65228027
	}
}

-- module import
-- require( 'strict' )

-- split separate items like numbers
function lu.splitItems( s, delimiters, defaultDelimiter )
	defaultDelimiter = defaultDelimiter or ','

	-- wrap delimiters with zero marks
	s = mw.ustring.gsub( s, defaultDelimiter, '\0%1\0' );

	-- substitude delimiters
	for i, delimiter in ipairs( delimiters ) do
		s = mw.ustring.gsub( s, delimiter, '\0%1\0' );
		-- remove zero marks from inside parentheses ()
		s =	s:gsub( '%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 lu.extractComment( s )
	local comment = ''
	if s:find( '(', 1, true ) then
		local t = s:gsub( '^.*(%b())$', '%1' )
		if t ~= s then
			comment = t
			s = s:gsub( '[%s%c]*%b()$', '' )
		end
	end
	return s, comment
end

return lu