Modul:Common: Unterschied zwischen den Versionen

Aus FürthWiki

Keine Bearbeitungszusammenfassung
(Verschiebung Kalender-Funktionen zu Modul:Kalender, - obsolete Funktionen)
 
(75 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
local p = {}
local p = {}


function p.AttributeTable(args)
function p.attributeTable(args)
-- Draws a simple Table that contains all arguments that are fed from the
-- Draws a simple Table that contains all arguments that are fed from the
-- template to the scribunto-model. For debugging only!
-- template to the scribunto-model. For debugging only!
t='<table class="wikitable">'
local t='<table class="wikitable">'
for i, v in pairs(args) do
for i, v in pairs(args) do
t = t .. '<tr><td>[[Attribut:' .. i .. '|' .. i .. ']]</td>' ..
t = t .. '<tr><td>[[Attribut:' .. i .. '|' .. i .. ']]</td>' ..
Zeile 11: Zeile 11:
t = t .. '</table>'
t = t .. '</table>'
return t
return t
end
function p.istJahrestag(frame)
local args = frame.args
    local title = mw.title.getCurrentTitle().text
    local tag, monat = title:match("^(%d%d?)%.%s*(%a+)$")
   
    -- Prüfe auf ein-/zweistellige Zahl durch Punkt gefolgt, opt. Leerzeichen
    if tag and monat then
        return frame.args[1] or "true"
    else
        return frame.args[2] or "false"
    end
end
function p.istJahreszahl(frame)
    local args = frame.args
    local title = mw.title.getCurrentTitle().text
    -- Prüfe auf Formate wie: 2024, -44, 44 v. Chr., 800 n. Chr.
    if title:match("^%-?%d+$") or title:match("^%d+%s+[vn]%.%s+Chr%.$") then
        return args[1] or "true"
    else
        return args[2] or "false"
    end
end
end


function p.getPageName(frame)
function p.getPageName(frame)
-- This is for unlinking semantic attributes with datatype site.
-- This is for unlinking semantic attributes with datatype site.
p = frame.args[1] or ""
local p = frame.args[1] or ""
-- Throw away all text after |
-- Throw away all text after |
t = mw.ustring.match(p, "([^|]+)")
local t = mw.ustring.match(p, "([^|]+)")
-- Throw away everything before [[:
-- Throw away everything before [[:
t = mw.ustring.match(t, "%[%[:(.+)")
t = mw.ustring.match(t, "%[%[:(.+)")
Zeile 51: Zeile 25:
end
end


function getMonthList()
function p.isEmpty(a)
-- Returns a numbered List of all monthnames
if type(a) == "table" then
return {
for _ in pairs(a) do
[1] = "Januar",
return false
[2] = "Februar",
end
[3] = "März",
return true
[4] = "April",
else
[5] = "Mai",
return a == '' or a == nil
[6] = "Juni",
end
[7] = "Juli",
[8] = "August",
[9] = "September",
[10] = "Oktober",
[11] = "November",
[12] = "Dezember"}
end
end


function getMonthnr(name)  
function p.len(t)
-- Returns the number of the given monthname
    -- Überprüfe, ob es ein String ist
months = getMonthList()
    if type(t) == "string" then
for nr, mn in pairs(months) do
        local count = 0
if mn == name then
        -- Zähle jedes Zeichen im String
return nr
        for _ in string.gmatch(t, ".") do
end
            count = count + 1
end
        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
end


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


function contains(list, str)
function p.get_key_for_value(t, value)
for i, v in ipairs(list) do
for k, v in pairs(t) do
if v == str then
if v == value then
return true
return k
end
end
end
end
return false
end
end


function isInteger(str)
function p.merge_tables(t1, t2)
  return not (str == "" or str:find("%D"))
-- 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
end


function get_key_for_value( t, value )
function p.append_tables(t1, t2)
  for k,v in pairs(t) do
-- append indexed/keyed table to another
    if v==value then return k end
-- indexes/keys gonna lost!
  end
-- the result is table with new indexes
  return nil
-- also see p.merge_tables()
end
local result={}
 
for _, v in pairs(t1) do
function rtrim(s)
table.insert(result, v)
return s:gsub("%s+$", "")
end
end
for _, v in pairs(t2) do
 
table.insert(result, v)
function ltrim(s)
return s:match("^%s*(.-)$")
end
 
function split(s, d)
if d==nil then d="%s" end
arr={}
n=0
for i in string.gmatch(s, "[^"..d.."]+") do
arr[n]=i
n=n+1
end
end
return arr
return result
end
end


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


function tableToString(tbl, indent)
function p.shallowcopy(orig)
-- Prints a LUA-Table as string. Very useful if you want to see whats inside
-- real-copy of a table
    indent = indent or 0
     local orig_type = type(orig)
     local toprint = string.rep(" ", indent) .. "{\n"
     local copy
     indent = indent + 2
     if orig_type == 'table' then
     for k, v in pairs(tbl) do
         copy = {}
        toprint = toprint .. string.rep(" ", indent)
         for orig_key, orig_value in pairs(orig) do
        if type(k) == "number" then
             copy[orig_key] = orig_value
            toprint = toprint .. "[" .. k .. "] = "
         elseif type(k) == "string" then
            toprint = toprint .. k .. " = "
         end
        if type(v) == "number" then
            toprint = toprint .. v .. ",\n"
        elseif type(v) == "string" then
             toprint = toprint .. "\"" .. v .. "\",\n"
        elseif type(v) == "table" then
            toprint = toprint .. tableToString(v, indent + 2) .. ",\n"
        else
            toprint = toprint .. "\"" .. tostring(v) .. "\",\n"
         end
         end
    else -- number, string, boolean, etc
        copy = orig
     end
     end
     toprint = toprint .. string.rep(" ", indent - 2) .. "}"
     return copy
    return toprint
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