# Path: philo/utils/templates.py
# LOADED_TEMPLATE_ATTR = '_philo_loaded_template'


import re
from django.core.exceptions import ValidationError
from django.template import Template, Parser, Lexer, TOKEN_BLOCK, TOKEN_VAR, TemplateSyntaxError
from django.utils import simplejson as json
from django.utils.html import escape, mark_safe
from django.utils.translation import ugettext_lazy as _
from philo.utils.templates import LOADED_TEMPLATE_ATTR
	
	"""
	def __init__(self, allow=None, disallow=None, secure=True):
		self.allow = allow
		self.disallow = disallow
		self.secure = secure
	
	def __call__(self, value):
		try:
			self.validate_template(value)
		except ValidationError:
			raise
		except Exception, e:
			if hasattr(e, 'source') and isinstance(e, TemplateSyntaxError):
				origin, (start, end) = e.source
				template_source = origin.reload()
				upto = 0
				for num, next in enumerate(linebreak_iter(template_source)):
					if start >= upto and end <= next:
						raise ValidationError(mark_safe("Template code invalid: \"%s\" (%s:%d).<br />%s" % (escape(template_source[start:end]), origin.loadname, num, e)))
					upto = next
			raise ValidationError("Template code invalid. Error was: %s: %s" % (e.__class__.__name__, e))
	
	def validate_template(self, template_string):
		# We want to tokenize like normal, then use a custom parser.
		lexer = Lexer(template_string, None)
		tokens = lexer.tokenize()
		parser = TemplateValidationParser(tokens, self.allow, self.disallow, self.secure)
		
		for node in parser.parse():

# Path: philo/utils/templates.py
# LOADED_TEMPLATE_ATTR = '_philo_loaded_template'


# Path: philo/validators.py
import re
from django.core.exceptions import ValidationError
from django.template import Template, Parser, Lexer, TOKEN_BLOCK, TOKEN_VAR, TemplateSyntaxError
from django.utils import simplejson as json
from django.utils.html import escape, mark_safe
from django.utils.translation import ugettext_lazy as _
from philo.utils.templates import LOADED_TEMPLATE_ATTR

	
	"""
	def __init__(self, allow=None, disallow=None, secure=True):
		self.allow = allow
		self.disallow = disallow
		self.secure = secure
	
	def __call__(self, value):
		try:
			self.validate_template(value)
		except ValidationError:
			raise
		except Exception, e:
			if hasattr(e, 'source') and isinstance(e, TemplateSyntaxError):
				origin, (start, end) = e.source
				template_source = origin.reload()
				upto = 0
				for num, next in enumerate(linebreak_iter(template_source)):
					if start >= upto and end <= next:
						raise ValidationError(mark_safe("Template code invalid: \"%s\" (%s:%d).<br />%s" % (escape(template_source[start:end]), origin.loadname, num, e)))
					upto = next
			raise ValidationError("Template code invalid. Error was: %s: %s" % (e.__class__.__name__, e))
	
	def validate_template(self, template_string):
		# We want to tokenize like normal, then use a custom parser.
		lexer = Lexer(template_string, None)
		tokens = lexer.tokenize()
		parser = TemplateValidationParser(tokens, self.allow, self.disallow, self.secure)
		
		for node in parser.parse():

			template = getattr(node, LOADED_TEMPLATE_ATTR, None)