Modul:Common: Unterschied zwischen den Versionen
Aus FürthWiki
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 87: | Zeile 87: | ||
end | end | ||
function isEmpty(a) | function p.isEmpty(a) | ||
return a == '' or a == nil | return a == '' or a == nil | ||
end | end | ||
Version vom 24. August 2025, 08:55 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.istJahrestag(frame)
local args = frame.args
local title = mw.title.getCurrentTitle().text
local tag, monat = istJahrestag(title)
if tag and monat then
return frame.args[1] or "true"
else
return frame.args[2] or ""
end
end
function 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)
local args = frame.args
local title = mw.title.getCurrentTitle().text
if istJahreszahl(title) then
return args[1] or "true"
else
return args[2] or ""
end
end
function 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 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
local months = getMonthList()
for nr, mn in pairs(months) do
if mn == name then
return nr
end
end
end
function p.isEmpty(a)
return a == '' or a == nil
end
function 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 contains(list, str)
-- Obsolet
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)
-- Obsolet
-- Removes all spaces at the end of a string
return s:gsub("%s+$", "")
end
function ltrim(s)
-- Obsolet
-- Removes all spaces at the start of a string
return s:match("^%s*(.-)$")
end
function strip(s)
-- Obsolet
-- Removes all spaces at the start and at the end of a string
return s:match("^%s*(.-)%s*$")
end
function merge_arrays(t1, t2)
-- merge tables that are arrays: {1,2,3} and {3,4,5}
local result = {}
for i = 1, #t1 do
table.insert(result, t1[i])
end
for i = 1, #t2 do
table.insert(result, t2[i])
end
return result
end
function 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 split(s, d)
-- Obsolet
if d == nil then d = "%s" end
local arr = {}
local n = 0
for i in string.gmatch(s, "([^" .. d .. "]+)") do
arr[n] = i
n = n + 1
end
return arr
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 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