Modul:Link utilities: Unterschied zwischen den Versionen

Aus skandinavien-wiki.net
Keine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
Zeile 12: Zeile 12:
for i = 1, #delimiters, 1 do
for i = 1, #delimiters, 1 do
s = mw.ustring.gsub( s, delimiters[i], '\0' .. delimiters[i] .. '\0' );
s = mw.ustring.gsub( s, delimiters[i], '\0' .. delimiters[i] .. '\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 = mw.ustring.gsub( s, '%b()', function( t ) return mw.ustring.gsub( t, '%z', '' ) end )
-- replace delimeters by commas
-- replace delimeters by the default delimiter
s = mw.ustring.gsub( s, '\0' .. delimiters[i] .. '\0', '\0' .. defaultDelimiter .. '\0' );
s = mw.ustring.gsub( s, '\0' .. delimiters[i] .. '\0', '\0' .. defaultDelimiter .. '\0' );
end
end

Version vom 7. Mai 2019, 12:32 Uhr

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

local lb = {}

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

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

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

function lb.extractComment( s )
	local comment = ''
	-- extract comment in parentheses
	-- remove spaces between number and comment
	local t = mw.ustring.gsub( s, '(.*)(%(.*%))$', '%2' )
	if t ~= s then
		comment = t
		s = mw.ustring.gsub( mw.ustring.gsub( s, '(.*)(%(.*%))$', '%1' ), '( +)$', '' )
	end
	return s, comment
end

function lb.errorInfo( catPrefix, aCat, aClass )
	aClass = aClass or 'error'
	local ns = mw.title.getCurrentTitle().namespace
	if ( ns == 0 ) or ( ns == 828 ) then
		return catPrefix .. aCat .. ']]' .. '<span class="' .. aClass .. '" title="'
			.. aCat .. '">' .. aCat .. '</span>'
	else
		return ''
	end
end

return lb