Zum Inhalt springen

Modul:aewi.WortstrukturMarkerEntfernen

aus AufEinWort, der freien Forschung

Die Dokumentation für dieses Modul kann unter Modul:aewi.WortstrukturMarkerEntfernen/Doku erstellt werden

local p = {}

-- Zeichen, die durch die Unicode-Zerlegung nicht automatisch
-- in Grundbuchstaben zerlegt werden.
local sonderzeichen = {
    ['ß'] = 'ss',
    ['ẞ'] = 'SS',
    ['Æ'] = 'AE',
    ['æ'] = 'ae',
    ['Œ'] = 'OE',
    ['œ'] = 'oe',
    ['Ø'] = 'O',
    ['ø'] = 'o',
    ['Ł'] = 'L',
    ['ł'] = 'l',
    ['Đ'] = 'D',
    ['đ'] = 'd',
    ['Ð'] = 'D',
    ['ð'] = 'd',
    ['Þ'] = 'TH',
    ['þ'] = 'th'
}

local function istKombinationszeichen(codepoint)
    return
        (codepoint >= 0x0300 and codepoint <= 0x036F) or
        (codepoint >= 0x1AB0 and codepoint <= 0x1AFF) or
        (codepoint >= 0x1DC0 and codepoint <= 0x1DFF) or
        (codepoint >= 0x20D0 and codepoint <= 0x20FF) or
        (codepoint >= 0xFE20 and codepoint <= 0xFE2F)
end

local function grundbuchstaben(text)
    -- Beispielsweise ä in a + kombinierte Umlautpunkte zerlegen.
    text = mw.ustring.toNFD(text) or text

    local ergebnis = {}

    for codepoint in mw.ustring.gcodepoint(text) do
        if not istKombinationszeichen(codepoint) then
            local zeichen = mw.ustring.char(codepoint)
            table.insert(ergebnis, sonderzeichen[zeichen] or zeichen)
        end
    end

    return table.concat(ergebnis)
end

function p.main(frame)
    local richtung = mw.text.trim(frame.args[1] or 'and')
    local anzahl = tonumber(mw.text.trim(frame.args[2] or '1')) or 1
    local modus = mw.text.trim(frame.args[3] or '')

    anzahl = math.max(0, math.floor(anzahl))

    local name = mw.title.getCurrentTitle().text
    local laenge = mw.ustring.len(name)
    local ergebnis

    if anzahl == 0 then
        ergebnis = name
    elseif anzahl >= laenge then
        ergebnis = ''
    elseif richtung == 'end' then
        ergebnis = mw.ustring.sub(name, 1, laenge - anzahl)
    else
        ergebnis = mw.ustring.sub(name, anzahl + 1)
    end

    if modus == 'sort' then
        ergebnis = grundbuchstaben(ergebnis)
    end

    return ergebnis
end

return p