Modul:Common
Aus FürthWiki
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.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
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.getMonthList()
-- Returns a numbered List of all monthnames
return {
[1] = "Januar",
[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
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
return nil
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 .. ' '
end
return s
end
function p.tableToString(tbl, indent)
-- Prints a LUA-Table as string. Very useful if you want to see whats inside
-- to do: still neccessary? => mw.logObject()
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
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.TemplateTranscludeWithArgs(frame)
local args = p.shallowcopy(frame:getParent().args) -- frame:getParent().args als Basis nehmen
local template = frame.args[1] -- enthält Vorlagen-Seitenname
for orig_key, orig_value in pairs(frame.args) do -- über alle args iterieren, um parentargs mit args anzureichern
if type(orig_key) == 'number' then -- bei numerierten Parametern...
if orig_key > 1 then -- ... eine Index-Verschiebung durchführen, weil ...
args[orig_key-1] = orig_value -- ... args[1] enthält Vorlagen-Seitenname, der
end -- nicht übernommen wird
else
args[orig_key] = orig_value -- bei nicht-Nummern-Index 1:1 kopieren
end
end
return frame:expandTemplate{title=template, args=args}
end
function p.isTrue(s)
-- testet String auf True-Werte verschiedenster Schreibweisen
local result = false
if not p.isEmpty(s) then
if 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
function p.returnStringCheck(s)
local t = ""
local l = string.len(s)
mw.smw.set({["LuaReturnLength"] = l})
if l > 100000 then -- aktuell Fehler ab ca. 450.000
t = t .. "[[Kategorie:Lua-String-Länge problematisch]]"
end
return t
end
return p