-
Notifications
You must be signed in to change notification settings - Fork 5
/
README
55 lines (38 loc) · 1.68 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
This is MapCSS parser I use in my OSM rendering. I've tried to implement spec
from http://wiki.openstreetmap.org/wiki/MapCSS but it's not very formal, so
the implementation may doesn't match with your vision of MapCSS.
This parser use PLY (http://www.dabeaz.com/ply) library.
== Dependency
sudo pip install ply
== Installation
sudo python setup.py install
== Layout
mapcss_parser/ast.py - Abstract Syntax Tree classes
mapcss_parser/lex.py - Lexer
mapcss_parser.py - MapCSS parser entry point
mapcss_parser/parse.py - Parser
test/parser_test.py - Unit tests of MapCSS parser
== Usage
Simple way to see MapCSS parser in action is use CLI:
> python mapcss-parser.py <YOUR_MAP_CSS_FILE>
This script will parse your file to AST, and build it again from AST to MapCSS. Not very useful, ah?
More complex example. I use this parser to convert MapCSS to the code. So I've just mixed-in
necessary methods directly to AST classes.
import sys
sys.path.insert(0,"../mapcss-parser")
from mapcss_parser import MapCSSParser
import ast
if len(sys.argv) != 2:
print "usage : mapcss.py inputfile"
raise SystemExit
content = open(sys.argv[1]).read()
parser = MapCSSParser(debug=False)
mapcss = parser.parse(content)
def mapcss_convert(self):
imports = "".join(map(lambda imp: propagate_import(imp.url).as_js, self.imports))
rules = "\n".join(map(lambda x: x.as_js(), self.rules))
return "%s%s" % (imports, rules)
ast.MapCSS.convert = mapcss_convert
... And so on for other 13 AST classes
print mapcss.convert()
If you have questions about MapCSS parser, you can find me on #osm and #osm_ru IRC channels, of e-mail me to <[email protected]>