Modul:Common: Unterschied zwischen den Versionen

Verschiebung Kalender-Funktionen zu Modul:Kalender, - obsolete Funktionen
Keine Bearbeitungszusammenfassung
(Verschiebung Kalender-Funktionen zu Modul:Kalender, - obsolete Funktionen)
 
(71 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 ""
    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 ""
    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)
-- Removes all spaces at the end of a string
end
return s:gsub("%s+$", "")
for _, v in pairs(t2) do
end
table.insert(result, v)
 
function ltrim(s)
-- Removes all spaces at the start of a string
return s:match("^%s*(.-)$")
end
 
function strip(s)
-- Removes all spaces at the start and at the end of a string
    return s:match("^%s*(.-)%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;'
end
end
return s
return s
end
function tableToString(tbl, indent)
-- Prints a LUA-Table as string. Very useful if you want to see whats inside
    indent = indent or 0
    local toprint = string.rep(" ", indent) .. "{\n"
    indent = indent + 2
    for k, v in pairs(tbl) do
        toprint = toprint .. string.rep(" ", indent)
        if type(k) == "number" then
            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
    toprint = toprint .. string.rep(" ", indent - 2) .. "}"
    return toprint
end
end


function p.shallowcopy(orig)
function p.shallowcopy(orig)
-- real-copy of a table
     local orig_type = type(orig)
     local orig_type = type(orig)
     local copy
     local copy
Zeile 176: Zeile 119:
     end
     end
     return copy
     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