Module:Trumpspeak

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

Please refer to Module:Readable

{{#invoke:Trumpspeak|translate|1946 Meximutts will be killed by Donald Trump to celebrate his 80th birthday, so YOU WILL not vote for xer.}}


shows


1q4b ɅɅexiɅɅVtts VVill be killed by DQnald TrVɅɅp tQ celebrate xer 8Qth birthday, sQ YQV VVILL nQt vQte fQr xer.


local g = {}

local str_map = {
    {'O', 'Q'},
    {'U', 'V'},
    {'W', 'VV'},
    {'M', 'ɅɅ'},
    {'o', 'Q'},
    {'u', 'V'},
    {'w', 'VV'},
    {'m', 'ɅɅ'},
    {'0', 'Q'},
    {'6', 'b'},
    {'9', 'q'}, -- Consider possible extensions: X/x = >< - C/c = < - N/n = Ʌ
}

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