91 lines
2.5 KiB
Python
Raw Normal View History

2012-01-28 14:52:09 +00:00
"""
"""
2012-02-27 19:22:52 +00:00
import re
from urllib.parse import quote as urlquote
2012-02-19 20:38:19 +00:00
from .node import Node
2012-02-25 17:08:08 +00:00
import lesscpy.lessc.utility as utility
2012-02-27 19:22:52 +00:00
import lesscpy.lessc.color as Color
2012-02-19 20:38:19 +00:00
class Call(Node):
2012-02-25 17:08:08 +00:00
def parse(self, scope):
2012-03-18 19:25:05 +00:00
if not self.parsed:
name = ''.join(self.tokens.pop(0))
parsed = self.process(self.tokens, scope)
if name == '%(':
name = 'sformat'
elif name == '~':
name = 'e'
color = Color.Color()
args = [t for t in parsed
if type(t) is not str or t not in '(),']
if hasattr(self, name):
try:
return getattr(self, name)(*args)
except ValueError:
pass
if hasattr(color, name):
try:
return getattr(color, name)(*args)
except ValueError:
pass
self.parsed = name + ''.join([p for p in parsed])
return self.parsed
2012-02-27 19:22:52 +00:00
def e(self, string):
""" Less Escape.
@param string: value
@return string
"""
return utility.destring(string.strip('~'))
def sformat(self, *args):
""" String format
@param list: values
@return string
"""
format = args[0]
items = []
m = re.findall('(%[asdA])', format)
i = 1
for n in m:
v = {
'%d' : int,
'%A' : urlquote,
'%s' : utility.destring,
}.get(n, str)(args[i])
items.append(v)
i += 1
format = format.replace('%A', '%s')
return format % tuple(items)
def increment(self, v):
""" Increment function
@param Mixed: value
@return: incremented value
"""
n, u = utility.analyze_number(v)
return utility.with_unit(n+1, u)
def decrement(self, v):
""" Decrement function
@param Mixed: value
@return: incremented value
"""
n, u = utility.analyze_number(v)
return utility.with_unit(n-1, u)
def add(self, *args):
""" Add integers
@param list: values
@return: int
"""
return sum([int(v) for v in args])
def round(self, v):
""" Round number
@param Mixed: value
@return: rounded value
"""
n, u = utility.analyze_number(v)
return utility.with_unit(round(float(n)), u)