Modul:Common: Unterschied zwischen den Versionen

Verschiebung Kalender-Funktionen zu Modul:Kalender, - obsolete Funktionen
(+ append_tables())
(Verschiebung Kalender-Funktionen zu Modul:Kalender, - obsolete Funktionen)
 
(21 dazwischenliegende Versionen von 2 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!
Zeile 11: Zeile 11:
t = t .. '</table>'
t = t .. '</table>'
return t
return t
end
function p.IstJahrestag(frame)
-- #invoke wrapper
local args = frame.args
    local title = mw.title.getCurrentTitle().text
    local tag, monat = p.istJahrestag(title)
   
    if tag and monat then
        return frame.args[1] or "true"
    else
        return frame.args[2] or ""
    end
end
function p.istJahrestag(s)
s = s or ""
-- Prüfe auf ein-/zweistellige Zahl durch Punkt gefolgt, opt. Leerzeichen
return s:match("^(%d%d?)%.%s*(%a+)$")
end
function p.IstJahreszahl(frame)
-- #invoke wrapper
    local args = frame.args
    local title = mw.title.getCurrentTitle().text
    if p.istJahreszahl(title) then
        return args[1] or "true"
    else
        return args[2] or ""
    end
end
function p.istJahreszahl(s)
s = s or ""
-- Prüfe auf Formate wie: 2024, -44, 44 v. Chr., 800 n. Chr.
return s:match("^%-?%d+$") or s:match("^%d+%s+[vn]%.%s+Chr%.$")
end
end


Zeile 62: Zeile 25:
end
end


function p.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",
[3] = "März",
[4] = "April",
[5] = "Mai",
[6] = "Juni",
[7] = "Juli",
[8] = "August",
[9] = "September",
[10] = "Oktober",
[11] = "November",
[12] = "Dezember"}
end
 
function p.getMonthnr(name)
-- Returns the number of the given monthname
local months = p.getMonthList()
for nr, mn in pairs(months) do
if mn == name then
return nr
end
end
return true
else
return a == '' or a == nil
end
end
end
function p.isEmpty(a)
return a == '' or a == nil
end
end


Zeile 119: Zeile 62:
end
end


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


Zeile 139: Zeile 83:


function p.append_tables(t1, t2)
function p.append_tables(t1, t2)
-- append indexes table to another
-- append indexed/keyed table to another
-- indexes/keys gonna lost!
-- the result is table with new indexes
-- also see p.merge_tables()
local result={}
local result={}
for _, v in pairs(t1) do
for _, v in pairs(t1) do
Zeile 147: Zeile 94:
table.insert(result, v)
table.insert(result, v)
end
end
mw.logObject(result)
return result
return result
end
end
Zeile 158: Zeile 104:
end
end
return s
return s
end
function p.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 200: Zeile 121:
end
end


function p.templateTranscludeWithArgs(frame)  
function p.isTrue(s)
local args = p.shallowcopy(frame:getParent().args) -- frame:getParent().args als Basis nehmen
-- testet String auf True-Werte verschiedenster Schreibweisen
local template = frame.args[1] -- enthält Vorlagen-Seitenname
local result = false
for orig_key, orig_value in pairs(frame.args) do -- über alle args iterieren, um parentargs mit args anzureichern
if s ~= nil then
if type(orig_key) == 'number' then -- bei numerierten Parametern...
if type(s) == "boolean" then
if orig_key > 1 then -- ... eine Index-Verschiebung durchführen, weil ...
result = s
args[orig_key-1] = orig_value -- ... args[1] enthält Vorlagen-Seitenname, der
elseif type(s) == "number" then
end -- nicht übernommen wird
result = (s ~= 0)
else
else
args[orig_key] = orig_value -- bei nicht-Nummern-Index 1:1 kopieren
s = tostring(s)
result = (s == "Ja" or s == "ja" or s == "Wahr" or s == "wahr" or s == "true")
end
end
end
end
return frame:expandTemplate{title=template, args=args}
return result
end
end


return p
return p