Skip to content

Commit

Permalink
merging WorldMaker's 'backstring' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredly committed Jun 19, 2010
2 parents 6faa4ed + 66b86c6 commit c94d64b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions clevercss/clevercss.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@
_number_re = re.compile(_r_number + '(?![a-zA-Z0-9_])')
_value_re = re.compile(r'(%s)(%s)(?![a-zA-Z0-9_])' % (_r_number, '|'.join(_units)))
_color_re = re.compile(r'#' + ('[a-fA-f0-9]{1,2}' * 3))
_backstring_re = re.compile(r'`([^`]*)`')
_string_re = re.compile('%s|([^\s*/();,.+$]+|\.(?!%s))+' % (_r_string, _r_call))
_url_re = re.compile(r'url\(\s*(%s|.*?)\s*\)' % _r_string)
_import_re = re.compile(r'\@import\s+url\(\s*"?(%s|.*?)"?\s*\)' % _r_string)
Expand Down Expand Up @@ -1115,6 +1116,20 @@ def evaluate(self, context):
return Color(args, lineno=self.lineno)


class Backstring(Literal):
"""
A string meant to be escaped directly to output.
"""
name = "backstring"

def __init__(self, nodes, lineno=None):
Expr.__init__(self, lineno)
self.nodes = nodes

def to_string(self, context):
return unicode(self.nodes)


class String(Literal):
name = 'string'

Expand Down Expand Up @@ -1600,6 +1615,7 @@ def process_string(m):
(_url_re, process('url', 1)),
(_import_re, process('import', 1)),
(_spritemap_re, process('spritemap', 1)),
(_backstring_re, process('backstring', 1)),
(_string_re, process_string),
(_var_re, lambda m: (m.group(1) or m.group(2), 'var')),
(_whitespace_re, None))
Expand Down Expand Up @@ -1707,6 +1723,9 @@ def primary(self, stream):
return RGB(tuple(args), lineno=stream.lineno)
else:
node = String('rgb')
elif token == 'backstring':
stream.next()
node = Backstring(value, lineno=stream.lineno)
elif token == 'string':
stream.next()
node = String(value, lineno=stream.lineno)
Expand Down
1 change: 1 addition & 0 deletions clevercss/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
'url': re.compile(r'url\(\s*(%s|.*?)\s*\)' % r_string),
'import': re.compile(r'\@import\s+url\(\s*"?(%s|.*?)"?\s*\)' % r_string),
'spritemap': re.compile(r'spritemap\(\s*(%s|.*?)\s*\)' % r_string),
'backstring': re.compile(r'`([^`]*)`'),
'var': re.compile(r'(?<!\\)\$(?:([a-zA-Z_][a-zA-Z0-9_]*)|'
r'\{([a-zA-Z_][a-zA-Z0-9_]*)\})'),
'call': re.compile(r'\.' + r_call)
Expand Down
4 changes: 4 additions & 0 deletions clevercss/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def process_string(m):
(consts.regex['url'], process('url', 1)),
(consts.regex['import'], process('import', 1)),
(consts.regex['spritemap'], process('spritemap', 1)),
(consts.regex['backstring'], process('backstring', 1)),
(consts.regex['string'], process_string),
(consts.regex['var'], lambda m: (m.group(1) or m.group(2), 'var')),
(consts.regex['whitespace'], None))
Expand Down Expand Up @@ -447,6 +448,9 @@ def primary(self, stream):
args.append(self.expr(stream, True))
stream.expect(')', 'op')
return expressions.RGBA(args)
elif token == 'backstring':
stream.next()
node = expressions.Backstring(value, lineno=stream.lineno)
elif token == 'string':
stream.next()
node = expressions.String(value, lineno=stream.lineno)
Expand Down
13 changes: 13 additions & 0 deletions clevercss/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,19 @@ def to_string(self, context):
args.append(value)
return 'rgba(%s)' % (', '.join(str(n) for n in args))

class Backstring(Literal):
"""
A string meant to be escaped directly to output.
"""
name = "backstring"

def __init__(self, nodes, lineno=None):
Expr.__init__(self, lineno)
self.nodes = nodes

def to_string(self, context):
return unicode(self.nodes)

class String(Literal):
name = 'string'

Expand Down
10 changes: 10 additions & 0 deletions tests/ccss_to_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def test_multiline_rule(self):
font-weight: bold;
}""").strip())

def backstring(self):
self.assertEqual(convert(dedent('''
div.round:
background-image: `-webkit-gradient(top left, bottom right, from(#fff), to(#000))`
''')), dedent('''\
div.round {
background-image: -webkit-gradient(top left, bottom right, from(#fff), to(#000));
}'''))


class LineIterTestCase(TestCase):
def test_comments(self):
line_iter = LineIterator(dedent(
Expand Down

0 comments on commit c94d64b

Please sign in to comment.