2012-01-28 14:52:09 +00:00
|
|
|
"""
|
2012-03-24 18:33:19 +00:00
|
|
|
Calls to builtin functions.
|
2012-01-28 14:52:09 +00:00
|
|
|
"""
|
2012-03-24 17:23:14 +00:00
|
|
|
import re, math
|
2012-02-27 19:22:52 +00:00
|
|
|
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-24 17:23:14 +00:00
|
|
|
"""
|
2012-03-24 18:33:19 +00:00
|
|
|
Parse Node within scope
|
2012-03-24 17:23:14 +00:00
|
|
|
"""
|
2012-03-18 19:25:05 +00:00
|
|
|
if not self.parsed:
|
2012-03-24 17:23:14 +00:00
|
|
|
name = ''.join(self.tokens[0])
|
|
|
|
parsed = self.process(self.tokens[1:], scope)
|
2012-03-18 19:25:05 +00:00
|
|
|
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
|
|
|
|
2012-03-24 18:33:19 +00:00
|
|
|
def e(self, *args):
|
2012-02-27 19:22:52 +00:00
|
|
|
""" Less Escape.
|
|
|
|
@param string: value
|
|
|
|
@return string
|
|
|
|
"""
|
2012-03-24 18:33:19 +00:00
|
|
|
if(len(args) > 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
|
|
|
return utility.destring(args[0].strip('~'))
|
2012-02-27 19:22:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2012-03-24 17:23:14 +00:00
|
|
|
def increment(self, *args):
|
2012-02-27 19:22:52 +00:00
|
|
|
""" Increment function
|
|
|
|
@param Mixed: value
|
|
|
|
@return: incremented value
|
|
|
|
"""
|
2012-03-24 17:23:14 +00:00
|
|
|
if(len(args) > 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
|
|
|
n, u = utility.analyze_number(args[0])
|
2012-02-27 19:22:52 +00:00
|
|
|
return utility.with_unit(n+1, u)
|
|
|
|
|
2012-03-24 17:23:14 +00:00
|
|
|
def decrement(self, *args):
|
2012-02-27 19:22:52 +00:00
|
|
|
""" Decrement function
|
|
|
|
@param Mixed: value
|
|
|
|
@return: incremented value
|
|
|
|
"""
|
2012-03-24 17:23:14 +00:00
|
|
|
if(len(args) > 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
|
|
|
n, u = utility.analyze_number(args[0])
|
2012-02-27 19:22:52 +00:00
|
|
|
return utility.with_unit(n-1, u)
|
|
|
|
|
|
|
|
def add(self, *args):
|
|
|
|
""" Add integers
|
|
|
|
@param list: values
|
|
|
|
@return: int
|
|
|
|
"""
|
2012-03-24 17:23:14 +00:00
|
|
|
if(len(args) <= 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
2012-02-27 19:22:52 +00:00
|
|
|
return sum([int(v) for v in args])
|
|
|
|
|
2012-03-24 17:23:14 +00:00
|
|
|
def round(self, *args):
|
2012-02-27 19:22:52 +00:00
|
|
|
""" Round number
|
|
|
|
@param Mixed: value
|
|
|
|
@return: rounded value
|
|
|
|
"""
|
2012-03-24 17:23:14 +00:00
|
|
|
if(len(args) > 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
|
|
|
n, u = utility.analyze_number(args[0])
|
2012-02-27 19:22:52 +00:00
|
|
|
return utility.with_unit(round(float(n)), u)
|
2012-03-24 17:23:14 +00:00
|
|
|
|
|
|
|
def ceil(self, *args):
|
2012-03-24 18:33:19 +00:00
|
|
|
""" Ceil number
|
2012-03-24 17:23:14 +00:00
|
|
|
"""
|
|
|
|
if(len(args) > 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
|
|
|
n, u = utility.analyze_number(args[0])
|
|
|
|
return utility.with_unit(math.ceil(n), u)
|
|
|
|
|
|
|
|
def floor(self, *args):
|
2012-03-24 18:33:19 +00:00
|
|
|
""" Floor number
|
2012-03-24 17:23:14 +00:00
|
|
|
"""
|
|
|
|
if(len(args) > 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
|
|
|
n, u = utility.analyze_number(args[0])
|
|
|
|
return utility.with_unit(math.floor(n), u)
|
|
|
|
|
|
|
|
def percentage(self, *args):
|
2012-03-24 18:33:19 +00:00
|
|
|
""" Return percentage value
|
2012-03-24 17:23:14 +00:00
|
|
|
"""
|
|
|
|
if(len(args) > 1):
|
|
|
|
raise SyntaxError('Wrong number of arguments')
|
|
|
|
n, u = utility.analyze_number(args[0])
|
|
|
|
n = int(n * 100.0)
|
|
|
|
u = '%'
|
|
|
|
return utility.with_unit(n, u)
|