|
|
| (62 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)
| |
| 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 | | end |
|
| |
|
| Zeile 60: |
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",
| |
| [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 |
| | return true |
| | else |
| | return a == '' or a == nil |
| end | | end |
| end | | end |
|
| |
|
| function isEmpty(a) | | function p.len(t) |
| return a == '' or a == nil
| |
| end
| |
| | |
| function len(t)
| |
| -- Überprüfe, ob es ein String ist | | -- Überprüfe, ob es ein String ist |
| if type(t) == "string" then | | if type(t) == "string" then |
| Zeile 100: |
Zeile 45: |
| end | | end |
| return count | | return count |
| | |
| -- Überprüfe, ob es eine Tabelle ist | | -- Überprüfe, ob es eine Tabelle ist |
| elseif type(t) == "table" then | | elseif type(t) == "table" then |
| Zeile 112: |
Zeile 58: |
| end | | end |
|
| |
|
| function contains(list, str) | | function p.isInteger(str) |
| for i, v in ipairs(list) do | | return not (str == "" or str:find("%D")) |
| if v == str then | | end |
| return true | | |
| | function p.get_key_for_value(t, value) |
| | for k, v in pairs(t) do |
| | if v == value then |
| | return k |
| end | | end |
| 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)
| |
| -- Removes all spaces at the end of a string
| |
| return s:gsub("%s+$", "")
| |
| end
| |
|
| |
| 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 | | end |
|
| |
|
| function merge_arrays(t1, t2) | | function p.merge_tables(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} | | -- merge tables that are key-value: {a=1, b=2} and {c=3, d=4} |
| local result={} | | local result={} |
| Zeile 171: |
Zeile 82: |
| end | | end |
|
| |
|
| function split(s, d) | | function p.append_tables(t1, t2) |
| local result = {}
| | -- append indexed/keyed table to another |
| local pattern = "(.-)" .. d -- .- bedeutet: alles bis zum ersten Vorkommen von delimiter
| | -- indexes/keys gonna lost! |
| local start = 1
| | -- the result is table with new indexes |
| while true do
| | -- also see p.merge_tables() |
| local part, pos = string.match(s, pattern, start)
| | local result={} |
| if part then
| | for _, v in pairs(t1) do |
| table.insert(result, part)
| | table.insert(result, v) |
| start = pos + 1
| | end |
| else
| | for _, v in pairs(t2) do |
| table.insert(result, string.sub(s, start))
| | table.insert(result, v) |
| break
| |
| end
| |
| end
| |
| return result
| |
| end
| |
| | |
| function split_old(s, d)
| |
| 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 | | end |
| return arr | | return result |
| end | | end |
|
| |
|
| Zeile 206: |
Zeile 104: |
| 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 246: |
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 |