How to use patterns to actually make your own Markup Language?
5 replies, posted
Well as the title states I want to do my own Markup language(things like <_red_></_red_> will make everything red inbetween them!)
So I tried to do my own testing etc looked on some lua pattern tutorials only, I dont get it work. Here is what I try:
[code]
local s = string.gmatch("<_blue_> Unknown </_blue_> " , "<_%a_>%a</_%a_>")
print(s())
[/code]
It prints out nothing. Does someone got an idea?
if it doesn't need to cascade, you can use patterns, use string.gsub
however, if you need them to cascade, you need a more complex parser
string.gmatch returns an iterator to be used with "for" btw
[QUOTE=commander204;17885008]Well as the title states I want to do my own Markup language(things like <_red_></_red_> will make everything red inbetween them!)
So I tried to do my own testing etc looked on some lua pattern tutorials only, I dont get it work. Here is what I try:
[code]
local s = string.gmatch("<_blue_> Unknown </_blue_> " , "<_%a_>%a</_%a_>")
print(s())
[/code]
It prints out nothing. Does someone got an idea?[/QUOTE]
[code]
local start,arg,finish = string.match("<_blue_>test :P</_blue_>" ,"<_([%w]+)_>([%w%s%p]*)</_([%w]+)_>")
print(start.."\n"..arg.."\n"..finish)
blue
test :P
blue
[/code]
omg, That shows how I suck at patterns. I still dont get it :O
[QUOTE=The-Stone;17895655][lua]
local start,arg,finish = string.match("<_blue_>test :P</_blue_>" ,"<_([%w]+)_>([%w%s%p]*)</_([%w]+)_>")
print(start.."\n"..arg.."\n"..finish)
blue
test :P
blue
[/lua][/QUOTE]
nice, but you want to make sure you get the matching closing tag, not just any of them:
[lua]local tag,arg = string.match("<_blue_>test :P</_blue_>" ,"<_([%w]+)_>([%w%s%p]*)</_%1_>")
print(tag.."\n"..arg)
blue
test :P[/lua]
I got caught up a bit and made this:
[lua]local text = "foo <blue>test :P</blue> bar"
local function Color(r,g,b)
return { r = r, g = g, b = b }
end
local tags = {
red = Color(255,0,0),
green = Color(0,255,0),
blue = Color(0,0,255),
}
defaultcolor = "Color(255,255,255)"
local output = { "chat.AddText("..defaultcolor..", " }
local last = 1
for before,tag,arg,after in text:gmatch("()<([%w]+)>([%w%s%p]*)</%2>()") do
textbefore = text:sub(last, before-1)
last = after
c = tags[tag]
if c then
-- a color for this command was found => display it.
table.insert(output, string.format("%q, Color(%d,%d,%d), %q, %s, ", textbefore, c.r,c.g,c.b, arg, defaultcolor))
else
-- command not found => display literally
table.insert(output, string.format("%q, ", text:sub(last, after-1)))
end
end
table.insert(output, string.format("%q)",text:sub(last)))
print(table.concat(output))
-- output: chat.AddText(Color(255,255,255), "foo ", Color(0,0,255), "test :P", Color(255,255,255), " bar")[/lua]
EDIT: fixed a small mistake
Nvm, TomyLobo got a nice example for colored chat.
Sorry, you need to Log In to post a reply to this thread.