Modul:Citation/utilities: Unterschied zwischen den Versionen

Aus skandinavien-wiki.net
KKeine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
Zeile 81: Zeile 81:
if list[ param ] then
if list[ param ] then
for k, v in ipairs( list[ param ] ) do
for k, v in ipairs( list[ param ] ) do
if uc.isSet( args[ v ] ) then
if cu.isSet( args[ v ] ) then
value = args[ v ]
value = args[ v ]
break
break

Version vom 26. März 2020, 06:41 Uhr

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

local cu = {}

local errorMsgs = {}

-- helper functions
function cu.isSet( param )
	return param and param ~= '';
end

-- math function round
function cu.round( num, decimalPlaces )
  local mult = 10^( decimalPlaces or 0 )
  return math.floor( num * mult + 0.5 ) / mult
end

-- adds error message to array
function cu.addErrorMsg( msg )
	table.insert( errorMsgs, msg )
end

-- make complete message from message array
function cu.getErrorMsgs()
	local i, j, result
	-- remove duplicates
	for i = #errorMsgs, 1, -1 do
		for j = 1, #errorMsgs - 1, 1 do
			if errorMsgs[ i ] == errorMsgs[ j ] then
				table.remove( errorMsgs, i )
				break
			end
		end
	end

	result = table.concat( errorMsgs, ' ' )
	if result ~= '' then
		result = result .. ' '
	end
	return result
end

-- get first item of a delimiter-separated list
function cu.getFirst( s, delimiter )
	local at = s:find( delimiter )
	if at then
		s = mw.text.trim( s:sub( 1, at - 1 ) )
	end
	return s
end

-- check if table contains the value
function cu.inArray( tab, val )
	if type( tab ) == 'string' then
		return tab == val
	end

	local index, value
	for index, value in ipairs( tab ) do
		if value == val then
			return true
		end
	end

	return false
end

-- convert values t from list if translated
function cu.getKey( t, list )
    local result = '', key, tab
    for key, tab in pairs( list ) do
        if cu.inArray( tab, t ) then
            result = key
            break
        end
    end
    return result
end

-- returns a single value from frame argument table
function cu.getArgValue( list, param, args )
	value = '', k, v
	if list[ param ] then
		for k, v in ipairs( list[ param ] ) do
			if cu.isSet( args[ v ] ) then
				value = args[ v ]
				break
			end
		end
	end
	return value
end

-- string cleanup
function cu.cleanupParameters( s, all )
	if not cu.isSet( s ) then
		return s
	end
	s = s:gsub( '[\009\010\013]', ' ' ) -- horizontal tab, line feed, carriage return
	s = s:gsub( '[%z%c]', '' ) -- control characters
	s = s:gsub( ' ', ' ' );
	s = s:gsub( '\226\128\138', ' ' ); -- hair space
	s = mw.ustring.gsub( s, '[\226\128\141\226\128\139\194\173]', '' ); -- zero-width joiner, zero-width space, soft hyphen
	s = mw.ustring.gsub( s, '%.%.%.', '…' )
	s = mw.ustring.gsub( s, '%.%.', '‥' )
	s = mw.ustring.gsub( s, '</*br[^/>]*/*>', '' )
	s = mw.ustring.gsub( s, '</*p[^/>]*/*>', '' )
	s = mw.ustring.gsub( s, '</*div[^/>]*/*>', '' )
	if all then
		s = mw.ustring.gsub( s, '%[%[[^%[%]]*|([^%[%]]*)%]%]', '%1' ) -- MediaWiki links
		s = mw.ustring.gsub( s, '%[%[([^%[%]]*)%]%]', '%1' )
		s = s:gsub( "''+", '' ) -- multiple apostrophes
		s = mw.ustring.gsub( s, '%[%a*:?//[^ ]+%s+([^%]]+)%]', '%1' ) -- web links
		s = mw.ustring.gsub( s, '%[mailto:[^ ]+%s+([^%]]+)%]', '%1' )
		s = mw.ustring.gsub( s, '%[%a*:?//([^%]]+)%]', '%1' )
		s = mw.ustring.gsub( s, '%[mailto:([^%]]+)%]', '%1' )
		s = mw.ustring.gsub( s, '</*span[^/>]*/*>', '' )
	end
	return s:gsub( '%s%s+', ' ' ) -- multiple spaces
end

return cu