8,756
edits
(nonxml support for modules) |
(cleaned up my very first lua module into something actually legible. also, now properly preserves whitespace if there is is a space between the color code and the actual text) |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local luatable = { | |||
['r'] = "#a64a2e", | ['r'] = "#a64a2e", | ||
['R'] = "#d74200", | ['R'] = "#d74200", | ||
Line 24: | Line 20: | ||
['o'] = "#f15f22", | ['o'] = "#f15f22", | ||
['O'] = "#e99f10" | ['O'] = "#e99f10" | ||
} | |||
--[Utility stuff]-- | |||
function normalize(string, type) | |||
type = type or '' | |||
local b = string.gsub(string,"(~J211)", "") | |||
if type == '' then | |||
b = string.gsub(b,"([{}])", "") | |||
elseif type == 'notxml' then | |||
b = string.gsub(b,"(\\n)","</br>") | |||
end | |||
return b | |||
end | end | ||
return | |||
function linebreaks(frame) | |||
local b = string.gsub(frame.args[1],"(~)","</br>") | |||
return b | |||
end | end | ||
function p. | --[ The main function. Takes the first argument and snips out stuff we don't need | ||
local b = | --[ depending on regextype. | ||
return | |||
function p.parse(frame, regextype) | |||
regextype = regextype or '' | |||
--[Determine regex type] | |||
local regex = "(&)(%w)(%s*)([^&\n]*)" | |||
if regextype == 'notxml' then | |||
regex = "(&)(%w)(%s*)([^&\n]*)" | |||
end | |||
local b = normalize(frame.args[1],regextype) | |||
local a = string.gsub(b, regex, function(_,color,space1,text) | |||
local hexcolor = luatable[color] | |||
if hexcolor == nil then | |||
error ("There was no specified color for color code: " .. color) | |||
end | |||
space1 = space1 or "" | |||
text = text or "" | |||
return space1 .. "<span style=\"color: " .. hexcolor .. ";\">" .. text .. "</span>" | |||
end) | |||
return a | |||
end | end | ||
function p.nonxmlparse(frame) | function p.nonxmlparse(frame) | ||
return p.parse(frame, 'notxml') | |||
end | end | ||
function p.moduleparse(frame) | function p.moduleparse(frame) | ||
return p.parse({["args"] = {frame}}) | |||
end | end | ||
function p.modulenonxmlparse(frame) | function p.modulenonxmlparse(frame) | ||
return p.nonxmlparse({["args"] = {frame}}) | |||
end | end | ||
return p | return p |