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)
	return a == '' or a == nil
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.spaces(frame)
	local n = tonumber(frame.args[1])
	local s = ""
	for i=0, n do
		s = s .. '&nbsp;'
	end
	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

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

function p.page_of_subobjekt(su)
    return string.find(su, '#' )
end

return p