Zuletzt bearbeitet vor einem Monat
von Xineohp1506

Helper

Die Dokumentation für dieses Modul kann unter Modul:Studio/Helper/Doku erstellt werden

local helper = {}
local getArgs = require('Module:Arguments').getArgs

-- Funktion zur Anpassung des DISPLAYTITLE über mw.ext.displaytitle
function helper.setDisplayTitle(frame)
    local args = getArgs(frame)
    local title = args["title"]

    if title and title ~= "" then
        local success, err = pcall(function()
            mw.ext.displaytitle.set(title)
        end)
        if not success then
            mw.log("Fehler beim Setzen des DISPLAYTITLE: " .. tostring(err))
        end
    end
end

-- Semantisches Attribut setzen, nur wenn Wert vorhanden und nicht leer
function helper.setIfExists(attr, value)
    if mw.smw and attr and value and value ~= "" then
        local success, err = pcall(function()
            mw.smw.set({ [attr] = value })
        end)
        if not success then
            mw.log("Fehler bei setIfExists: " .. tostring(err))
        end
    end
end

-- set with return results
function helper.setProperty(frame)
    if not mw.smw then
        return "mw.smw module not found"
    end

    local result = mw.smw.set(frame.args)
    if result == true then
        return
    else
        return 'An error occurred during the storage process. Message reads ' .. result.error
    end
end

-- Funktion zur Hinzufügung von Kategorien mit Sortkey
function helper.addCategories(frame)
    local args = getArgs(frame)
    local categories = args["categories"] or {}
    local sortkey = args["sortkey"] or mw.title.getCurrentTitle().text

    if type(categories) == "string" then
        categories = mw.text.split(categories, ",")
    end
    if type(categories) ~= "table" then
        return "Ungültige Kategorienliste."
    end

    local output = ""
    for _, category in ipairs(categories) do
        if category and category ~= "" then
            output = output .. "[[Kategorie:" .. category .. "|" .. sortkey .. "]]\n"
        end
    end

    return output
end

-- String von Sonderzeichen bereinigen
function helper.cleanString(input)
    if not input or input == "" then
        return ""
    end

    return input
        :gsub("[%c]", "")         -- Steuerzeichen
        :gsub("[\"']", "")        -- Anführungszeichen
        :gsub("[%p&&[^:/]]", "")  -- Satzzeichen außer : und /
        :gsub("%s+", " ")         -- Mehrfache Leerzeichen
        :gsub("^%s*(.-)%s*$", "%1")
        :gsub("[:/]", " - ")
end

-- Text bereinigen und ggf. kürzen
function helper.cleanAndTruncate(text, truncateLength)
    if not text or text == "" then
        return ""
    end

    text = mw.text.killMarkers(text)
        :gsub(' ', ' ')
        :gsub('<br ?/?>', ', ')
        :gsub('<span.->(.-)</span>', '%1')
        :gsub('<i.->(.-)</i>', '%1')
        :gsub('<b.->(.-)</b>', '%1')
        :gsub('<em.->(.-)</em>', '%1')
        :gsub('<strong.->(.-)</strong>', '%1')
        :gsub('<sub.->(.-)</sub>', '%1')
        :gsub('<sup.->(.-)</sup>', '%1')
        :gsub('<u.->(.-)</u>', '%1')
        :gsub('<.->.-<.->', '')
        :gsub('<.->', '')
        :gsub('%[%[%s*[Ff][Ii][Ll][Ee]%s*:.-%]%]', '')
        :gsub('%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:.-%]%]', '')
        :gsub('%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:.-%]%]', '')
        :gsub('%[%[[^%]]-|', '')
        :gsub('([^%[])%[[^%[%]][^%]]-%s', '%1')
        :gsub('^%[[^%[%]][^%]]-%s', '')
        :gsub('[%[%]]', '')
        :gsub("'''''", "")
        :gsub("'''?", "")
        :gsub('----+', '')
        :gsub("^%s+", "")
        :gsub("%s+$", "")
        :gsub("%s+", " ")

    if truncateLength and tonumber(truncateLength) then
        text = mw.text.truncate(text, tonumber(truncateLength), '…', true)
    end

    return text
end

-- Funktion zur Erzeugung von Videoplattform-Links
function helper.getVideoLink(service, id)
    if not service or not id then
        return nil
    end

    local link = ""
    if service == "youtube" then
        link = "https://www.youtube.com/watch?v=" .. id
    elseif service == "twitch" then
        link = "https://www.twitch.tv/videos/" .. id
    elseif service == "vimeo" then
        link = "https://vimeo.com/" .. id
    else
        return nil
    end

    return link
end

function helper.getIcon(icon)
	if not icon or icon == "" then return "" end
	return mw.getCurrentFrame():preprocess('{{#fas:' .. icon .. '}} ')
end

function helper.getIconRaw(icon)
	if not icon or icon == "" then return "" end
	return '{{#fas:' .. icon .. '}} '
end

return helper