diff options
Diffstat (limited to 'CSSParser.py')
-rwxr-xr-x | CSSParser.py | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/CSSParser.py b/CSSParser.py new file mode 100755 index 0000000..72c643e --- /dev/null +++ b/CSSParser.py | |||
@@ -0,0 +1,155 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | import sys, re | ||
4 | |||
5 | class CSSUtils: | ||
6 | ColorRegex = re.compile('', re.I | re.M | re.U) | ||
7 | |||
8 | @staticmethod | ||
9 | def compress_color(color): | ||
10 | pass | ||
11 | |||
12 | @staticmethod | ||
13 | def is_color(color): | ||
14 | pass | ||
15 | |||
16 | class CSSContainer: | ||
17 | selector = '' | ||
18 | |||
19 | def __init__(self): | ||
20 | pass | ||
21 | |||
22 | class CSSParser: | ||
23 | ValidProperties = ( | ||
24 | 'azimuth', | ||
25 | 'background', | ||
26 | 'background-color', | ||
27 | 'background-image', | ||
28 | 'background-repeat', | ||
29 | 'background-attachment', | ||
30 | 'background-position', | ||
31 | 'border', | ||
32 | 'border-top', | ||
33 | 'border-right', | ||
34 | 'border-left', | ||
35 | 'border-bottom', | ||
36 | 'border-color', | ||
37 | 'border-top-color', | ||
38 | 'border-right-color', | ||
39 | 'border-bottom-color', | ||
40 | 'border-left-color', | ||
41 | 'border-style', | ||
42 | 'border-top-style', | ||
43 | 'border-right-style', | ||
44 | 'border-bottom-style', | ||
45 | 'border-left-style', | ||
46 | 'border-width', | ||
47 | 'border-top-width', | ||
48 | 'border-left-width', | ||
49 | 'border-right-width', | ||
50 | 'border-bottom-width', | ||
51 | 'border-collapse', | ||
52 | 'border-spacing', | ||
53 | 'bottom', | ||
54 | 'clear', | ||
55 | 'clip', | ||
56 | 'color', | ||
57 | 'cursor', | ||
58 | 'direction', | ||
59 | 'display', | ||
60 | 'float', | ||
61 | 'font', | ||
62 | 'font-family', | ||
63 | 'font-size', | ||
64 | 'font-style', | ||
65 | 'font-variant', | ||
66 | 'font-weight', | ||
67 | 'height', | ||
68 | 'left', | ||
69 | 'letter-spacing', | ||
70 | 'line-height', | ||
71 | 'list-style', | ||
72 | 'list-style-image', | ||
73 | 'list-style-position', | ||
74 | 'list-style-type', | ||
75 | 'margin', | ||
76 | 'margin-top', | ||
77 | 'margin-right', | ||
78 | 'margin-bottom', | ||
79 | 'margin-left', | ||
80 | 'marker-offset', | ||
81 | 'marks', | ||
82 | 'overflow', | ||
83 | 'padding', | ||
84 | 'padding-top', | ||
85 | 'padding-bottom', | ||
86 | 'padding-right', | ||
87 | 'padding-left', | ||
88 | 'page-break-before', | ||
89 | 'position', | ||
90 | 'right', | ||
91 | 'size', | ||
92 | 'src', | ||
93 | 'table-layout', | ||
94 | 'text-align', | ||
95 | 'text-decoration', | ||
96 | 'text-indent', | ||
97 | 'text-transform', | ||
98 | 'top', | ||
99 | 'vertical-align', | ||
100 | 'visibility', | ||
101 | 'white-space', | ||
102 | 'width', | ||
103 | 'z-index' | ||
104 | ) | ||
105 | |||
106 | CSSRegex = re.compile(r'([^{]+)\s?{\s?([^}]*)\s?}', re.I | re.M | re.U) | ||
107 | PropertyRegex = re.compile(r'([^:]+):([^;]+);', re.I | re.M | re.U) | ||
108 | CommentRegex = re.compile(r'/\*(.|[\r\n])*?\*/', re.I | re.M | re.U) | ||
109 | |||
110 | def __init__(self): | ||
111 | pass | ||
112 | |||
113 | def parse(self): | ||
114 | pass | ||
115 | |||
116 | def duplicate_check(self): | ||
117 | pass | ||
118 | |||
119 | def load_file(filename): | ||
120 | """ Loads a file line by line into a variable and returns it.""" | ||
121 | infile = '' | ||
122 | |||
123 | try: | ||
124 | theFile = file(filename, "r") | ||
125 | except IOError: | ||
126 | sys.exit() | ||
127 | |||
128 | for line in theFile: | ||
129 | infile += line | ||
130 | |||
131 | return infile | ||
132 | |||
133 | if __name__ == '__main__': | ||
134 | output = CSSParser.CommentRegex.sub(' ', load_file(sys.argv[1])) | ||
135 | output = CSSParser.CSSRegex.findall(output) | ||
136 | |||
137 | containers = 0 | ||
138 | errors = 0 | ||
139 | |||
140 | for item in output: | ||
141 | containers += 1 | ||
142 | properties = re.sub('\s+', ' ', item[1]) | ||
143 | proparray = CSSParser.PropertyRegex.findall(properties) | ||
144 | container = item[0].strip() | ||
145 | |||
146 | #print "Container: %s" % item[0].strip() | ||
147 | #print "Contents: %s" % properties | ||
148 | print "Property Array: %s \n\n" % proparray | ||
149 | |||
150 | for item in proparray: | ||
151 | if item[0].strip().lower() not in CSSParser.ValidProperties: | ||
152 | errors += 1 | ||
153 | print "WARNING: Invalid property '%s' in container '%s'" % (item[0].strip(), container) | ||
154 | |||
155 | print "---\n\nFound %s containers and %s errors" % (containers, errors) | ||