[Pyparsing] parsing SVG styles
Brought to you by:
ptmcg
From: Donn I. <don...@gm...> - 2007-12-09 13:00:51
|
Hello again, I have now spent almost 3 hours on this and I've also looked at the examples and read the pdfs, but I just can't get this going. I actually find the docs and the adventure example too complicated -- I'm a simple sort. I'll post my code and hope for mercy -- as I've been told to rtfm before :) I'm trying to "pick-out" certain keywords (and args) from a string (style node in an SVG file) from amidst a babble of noise and just record those for later use. fill:#[6 hex nums]; fill-opacity:#[float or int]; stroke:#[6 hex nums]; stoke-width:#[float or int]; This is my latest test: # Cover [1] or [0.587] floatOrInt = Combine(Word(nums) + Optional(Literal(".") + Word(nums))) # Cover any amount of hex, [ab][abcf] hexNums = Word(hexnums) # A semi-colon seps commands semi = Literal(";").suppress() # hacking the commands FILL_command = Optional("fill:#")# + Group(hexNums + semi) FILLOPACITY_command = "fill-opacity:#" + floatOrInt + semi STROKECOLOR_command = "stroke:#" + hexNums + semi STROKEWIDTH_command = "stroke-width:#" + floatOrInt + semi # Trying to sum them up. Remarked down to one for testing stylecommand = FILL_command# | FILLOPACITY_command | STROKECOLOR_command | STROKEWIDTH_command # Hacked during tests, tried to simplify. phrase2 = stylecommand#OneOrMore(Group(stylecommand)) #The test string style="opacity:1;color:#000000;fill:#6bdc23;fill-opacity:0.4611111;fill-rule:nonzero;stroke:#ff0000;stroke-width:6;stroke-linecap:butt;stroke-linejoin:miter;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:2;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" tokensStyle = phrase2.parseString(style) # I also tried scanString() print tokensStyle # Trying to get a result. for a in tokensStyle: print a ## for command in a: ## print ":",command \d |