Modul:Common: Unterschied zwischen den Versionen

(Die Seite wurde neu angelegt: „local p = {} function p.AttributeTable( args ) t='<table class="wikitable">' for i,v in pairs(args) do t = t .. '<tr><td>' .. i .. '</td><td>' .. v .. '</…“)
 
(Verschiebung Kalender-Funktionen zu Modul:Kalender, - obsolete Funktionen)
 
(152 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
local p = {}
local p = {}


function p.AttributeTable( args )
function p.attributeTable(args)
t='<table class="wikitable">'
-- Draws a simple Table that contains all arguments that are fed from the
for i,v in pairs(args) do
-- template to the scribunto-model. For debugging only!
t = t .. '<tr><td>' .. i .. '</td><td>' .. v .. '</td></tr>'
local t='<table class="wikitable">'
for i, v in pairs(args) do
t = t .. '<tr><td>[[Attribut:' .. i .. '|' .. i .. ']]</td>' ..
'<td>' .. v .. '</td></tr>'
end
end
t = t .. '</table>'
t = t .. '</table>'
Zeile 10: Zeile 13:
end
end


function isEmpty(a)
function p.getPageName(frame)
return a == '' or a == nil
-- This is for unlinking semantic attributes with datatype site.
local p = frame.args[1] or ""
-- Throw away all text after |
local t = mw.ustring.match(p, "([^|]+)")
-- Throw away everything before [[:
t = mw.ustring.match(t, "%[%[:(.+)")
-- Next line was to check, what that span of subobjects does
--t = '<span class="smw-subobject-entity">'..t..'</span>'
return t
end
 
function p.isEmpty(a)
if type(a) == "table" then
for _ in pairs(a) do
return false
end
return true
else
return a == '' or a == nil
end
end
 
function p.len(t)
    -- Überprüfe, ob es ein String ist
    if type(t) == "string" then
        local count = 0
        -- Zähle jedes Zeichen im String
        for _ in string.gmatch(t, ".") do
            count = count + 1
        end
        return count
       
    -- Überprüfe, ob es eine Tabelle ist
    elseif type(t) == "table" then
        local count = 0
        for _ in pairs(t) do
            count = count + 1
        end
        return count
    else
        return nil  -- Falls der Typ nicht unterstützt wird
    end
end
 
function p.isInteger(str)
return not (str == "" or str:find("%D"))
end
 
function p.get_key_for_value(t, value)
for k, v in pairs(t) do
if v == value then
return k
end
end
end
 
function p.merge_tables(t1, t2)
-- merge tables that are key-value: {a=1, b=2} and {c=3, d=4}
local result={}
for k, v in pairs(t1) do
result[k] = v
end
for k, v in pairs(t2) do
result[k] = v
end
return result
end
 
function p.append_tables(t1, t2)
-- append indexed/keyed table to another
-- indexes/keys gonna lost!
-- the result is table with new indexes
-- also see p.merge_tables()
local result={}
for _, v in pairs(t1) do
table.insert(result, v)
end
for _, v in pairs(t2) do
table.insert(result, v)
end
return result
end
 
function p.spaces(frame)
local n = tonumber(frame.args[1])
local s = ""
for i=0, n do
s = s .. '&nbsp;'
end
return s
end
 
function p.shallowcopy(orig)
-- real-copy of a table
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in pairs(orig) do
            copy[orig_key] = orig_value
        end
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end
 
function p.isTrue(s)
-- testet String auf True-Werte verschiedenster Schreibweisen
local result = false
if s ~= nil then
if type(s) == "boolean" then
result = s
elseif type(s) == "number" then
result = (s ~= 0)
else
s = tostring(s)
result = (s == "Ja" or s == "ja" or s == "Wahr" or s == "wahr" or s == "true")
end
end
return result
end
end


return p
return p

Aktuelle Version vom 29. Dezember 2025, 09:58 Uhr

Das Commons-Modul ist ein reines Import-Modul, welches immer wieder kehrende Funktionen beinhaltet, die über das Ganze Projekt hinweg immer wieder gebraucht werden.

shallowcopy()

Erzeugt eine echte Kopie einer Tabelle.

isTrue()

Testet String auf True-Werte verschiedenster Schreibweisen.

Unterseiten


local p = {}

function p.attributeTable(args)
	-- Draws a simple Table that contains all arguments that are fed from the
	-- template to the scribunto-model. For debugging only!
	local t='<table class="wikitable">'
	for i, v in pairs(args) do
		t = t .. '<tr><td>[[Attribut:' .. i .. '|' .. i .. ']]</td>' ..
				 '<td>' .. v .. '</td></tr>'
	end
	t = t .. '</table>'
	return t
end

function p.getPageName(frame)
	-- This is for unlinking semantic attributes with datatype site.
	local p = frame.args[1] or ""
	-- Throw away all text after |
	local t = mw.ustring.match(p, "([^|]+)")
	-- Throw away everything before [[:
	t = mw.ustring.match(t, "%[%[:(.+)")
	-- Next line was to check, what that span of subobjects does
	--t = '<span class="smw-subobject-entity">'..t..'</span>'
	return t
end

function p.isEmpty(a)
	if type(a) == "table" then
		for _ in pairs(a) do
			return false
		end
		return true
	else
		return a == '' or a == nil
	end
end

function p.len(t)
    -- Überprüfe, ob es ein String ist
    if type(t) == "string" then
        local count = 0
        -- Zähle jedes Zeichen im String
        for _ in string.gmatch(t, ".") do
            count = count + 1
        end
        return count
        
    -- Überprüfe, ob es eine Tabelle ist
    elseif type(t) == "table" then
        local count = 0
        for _ in pairs(t) do
            count = count + 1
        end
        return count
    else
        return nil  -- Falls der Typ nicht unterstützt wird
    end
end

function p.isInteger(str)
	return not (str == "" or str:find("%D"))
end

function p.get_key_for_value(t, value)
	for k, v in pairs(t) do
		if v == value then
			return k
		end
	end
end

function p.merge_tables(t1, t2)
	-- merge tables that are key-value: {a=1, b=2} and {c=3, d=4}
	local result={}
	for k, v in pairs(t1) do
		result[k] = v
	end
	for k, v in pairs(t2) do
		result[k] = v
	end
	return result
end

function p.append_tables(t1, t2)
	-- append indexed/keyed table to another
	-- indexes/keys gonna lost!
	-- the result is table with new indexes
	-- also see p.merge_tables()
	local result={}
	for _, v in pairs(t1) do
		table.insert(result, v)
	end
	for _, v in pairs(t2) do
		table.insert(result, v)
	end
	return result
end

function p.spaces(frame)
	local n = tonumber(frame.args[1])
	local s = ""
	for i=0, n do
		s = s .. '&nbsp;'
	end
	return s
end

function p.shallowcopy(orig)
	-- real-copy of a table
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in pairs(orig) do
            copy[orig_key] = orig_value
        end
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end

function p.isTrue(s)
	-- testet String auf True-Werte verschiedenster Schreibweisen
	local result = false
	if s ~= nil then
		if type(s) == "boolean" then
			result = s
		elseif type(s) == "number" then
			result = (s ~= 0)
		else
			s = tostring(s)
			result = (s == "Ja" or s == "ja" or s == "Wahr" or s == "wahr" or s == "true")
		end
	end
	return result
end

return p