Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Created July 17, 2015 07:54
Show Gist options
  • Save skatenerd/b71671adbbdfd16a77c4 to your computer and use it in GitHub Desktop.
Save skatenerd/b71671adbbdfd16a77c4 to your computer and use it in GitHub Desktop.
pyparsing is pretty cool
import pyparsing
class Word(object):
def __init__(self, body):
self.body = body
def __repr__(self):
return self.body[0]
class Tag(object):
def __init__(self, body):
self.body = body[1]
def __repr__(self):
return "{%s}"%self.body
class Newline(object):
def __init__(self, body):
self.body = body
def __repr__(self):
return "\n"
customword = pyparsing.Regex("[^<^>]+")
customword.setParseAction(Word)
tag="<"+customword+">"
tag.setParseAction(Tag)
newline = "<br" + pyparsing.Optional("/") + ">"
newline.setParseAction(Newline)
shit=pyparsing.OneOrMore(pyparsing.Or([newline, tag, customword]))
print "".join(map(repr, shit.parseString('foo <greet> <br/><br> baz <farewell>'))).format(greet='hi', farewell='bye')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment