Module:Trumpspeak
From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search
Please refer to Module:Readable
{ { #invoke:Trumpspeak|translate|yuor text is winnerer } }
shows
yvQr text is vvinnerer
local g = {}
local str_map = {
{'M', 'ɅɅ'},
{'O', 'Q'},
{'U', 'V'},
{'W', 'VV'},
{'u', 'v'},
{'o', 'Q'},
{'w', 'vv'},
{'m', 'ɅɅ'},
}
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