Modul:Common: Unterschied zwischen den Versionen
Aus FürthWiki
(+ shallowcopy) |
(fix p.shallowcopy) |
||
| Zeile 157: | Zeile 157: | ||
end | end | ||
function shallowcopy(orig) | function p.shallowcopy(orig) | ||
local orig_type = type(orig) | local orig_type = type(orig) | ||
local copy | local copy | ||
Version vom 13. August 2025, 19:20 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!
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)
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
function p.getPageName(frame)
-- This is for unlinking semantic attributes with datatype site.
p = frame.args[1] or ""
-- Throw away all text after |
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 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 getMonthnr(name)
-- Returns the number of the given monthname
months = getMonthList()
for nr, mn in pairs(months) do
if mn == name then
return nr
end
end
end
function isEmpty(a)
return a == '' or a == nil
end
function contains(list, str)
for i, v in ipairs(list) do
if v == str then
return true
end
end
return false
end
function isInteger(str)
return not (str == "" or str:find("%D"))
end
function 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 rtrim(s)
return s:gsub("%s+$", "")
end
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
return arr
end
function p.spaces(frame)
n = tonumber(frame.args[1])
s = ""
for i=0, n do
s = s .. ' '
end
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
function p.shallowcopy(orig)
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
return p