Module:Readable

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search

This replaces character strings with other character strings.Do know that it prioritizes translating the strings with respect to length. aka if you have "amongusa -> a" and "amongus"->b it will turn all instances of amongusa into a and then amongus into b. not the reverse. I will add an option to toggle on and off modifying anything inside parenthesis and links and refs etc and You can make custom ones if you want. Current Readable modules are Module:Trumpspeak

{ { #invoke:Readable|translate|your text } }


aka


{ { #invoke:Trumpspeak|translate|Donald Trumpfo did not WIN} }

DQnald TrvɅɅpfQ did nQt VVIN



>naming your variables p


local g = {}

local str_map = {
    {'amogusa', 'hia'},
    {'amogus', 'hi'},
    {'RE', 'R'},
    {'re', 'r'},
}

table.sort(str_map, function(a, b)
    return #a[1] > #b[1]  -- Sort by pattern length descending
end)

function g.translate(frame)
    local input = frame.args[1] or ''
    local result = {}
    
    -- Split the text while preserving content inside {} and []
    local pos = 1
    local length = mw.ustring.len(input)
    
    while pos <= length do
        -- Check if we're at the start of a protected block
        local char = mw.ustring.sub(input, pos, pos)
        
        if char == '{' then
            -- Find matching closing brace
            local bracket_count = 1
            local start_pos = pos
            pos = pos + 1
            
            while pos <= length and bracket_count > 0 do
                local next_char = mw.ustring.sub(input, pos, pos)
                if next_char == '{' then
                    bracket_count = bracket_count + 1
                elseif next_char == '}' then
                    bracket_count = bracket_count - 1
                end
                pos = pos + 1
            end
            
            -- Extract the protected block and add it unchanged
            local protected = mw.ustring.sub(input, start_pos, pos - 1)
            table.insert(result, protected)
            
        elseif char == '[' then
            -- Find matching closing bracket
            local bracket_count = 1
            local start_pos = pos
            pos = pos + 1
            
            while pos <= length and bracket_count > 0 do
                local next_char = mw.ustring.sub(input, pos, pos)
                if next_char == '[' then
                    bracket_count = bracket_count + 1
                elseif next_char == ']' then
                    bracket_count = bracket_count - 1
                end
                pos = pos + 1
            end
            
            -- Extract the protected block and add it unchanged
            local protected = mw.ustring.sub(input, start_pos, pos - 1)
            table.insert(result, protected)
            
        else
            -- Find regular text until next protected block
            local start_pos = pos
            
            while pos <= length do
                local next_char = mw.ustring.sub(input, pos, pos)
                if next_char == '{' or next_char == '[' then
                    break
                end
                pos = pos + 1
            end
            
            -- Extract and translate the regular text
            local text = mw.ustring.sub(input, start_pos, pos - 1)
            
            -- Apply translations to this text segment
            for _, pair in ipairs(str_map) do
                local pattern = pair[1]
                local replacement = pair[2]
                text = mw.ustring.gsub(text, pattern, replacement)
            end
            
            table.insert(result, text)
        end
    end
    
    return table.concat(result)
end

return g