def parse_mixin(less, parent=None, **kwargs): match = MIXIN.match(less) if not match: raise ValueError() code = match.group() contents = None if match.group('nested'): matched_length = len(match.group()) remaining_less = less[matched_length:] contents = parse_nested(remaining_less) code += contents + '}' params = parse_params(match.group('params')) if contents: for param in params: if param['value'] and not param['name']: param['name'] = param['value'] param['value'] = None name = match.group('name') if match.group('nested') and not match.group('param_detect'): raise ValueError() return Mixin(parent=parent, code=code, name=name, params=params, contents=contents)
def parse_mixin(less, parent=None, **kwargs): match = MIXIN.match(less) if not match: raise ValueError() code = match.group() contents = None if match.group("nested"): matched_length = len(match.group()) remaining_less = less[matched_length:] contents = parse_nested(remaining_less) code += contents + "}" params = parse_params(match.group("params")) if contents: for param in params: if param["value"] and not param["name"]: param["name"] = param["value"] param["value"] = None name = match.group("name") if match.group("nested") and not match.group("param_detect"): raise ValueError() return Mixin(parent=parent, code=code, name=name, params=params, contents=contents)
def parse_media(less, parent=None, **kwargs): match = MEDIA.match(less) if not match: raise ValueError() media = [media.strip() for media in match.group('media').split(',')] matched_length = len(match.group()) remaining_less = less[matched_length:] contents = parse_nested(remaining_less) code = match.group() + contents + '}' return Media(code=code, media=media, contents=contents, parent=parent)
def parse_selector(less, parent=None, **kwargs): match = SELECTOR.match(less) if not match: raise ValueError() names = match.group('names') if names.startswith('@media'): raise ValueError names = [name.strip() for name in names.split(',')] matched_length = len(match.group()) remaining_less = less[matched_length:] contents = parse_nested(remaining_less) code = match.group() + contents + '}' return Selector(code=code, names=names, contents=contents, parent=parent)
def test_bundle(self): self.assertEqual(".b { :h { } } .t { ... } .c { ... } ", parse_nested(".b { :h { } } .t { ... } .c { ... } }"))
def test_braces_in_quotes(self): self.assertEqual('quotes: "{" "}"', parse_nested('quotes: "{" "}"}'))
def test_triple_depth_spaced(self): self.assertEqual(' { { a } } ', parse_nested(' { { a } } } '))
def test_single_depth_spaced(self): self.assertEqual(' a ', parse_nested(' a } '))
def test_open_brace_in_quotes(self): self.assertEqual('quotes: "{"', parse_nested('quotes: "{"}'))
def test_double_depth_spaced(self): self.assertEqual(' { a } ', parse_nested(' { a } } '))