Ejemplo n.º 1
0
		public SyntaxTreeNode Parse(string templateId, TextReader templateReader, Type modelType, IMemberLocator memberLocator, IHelperHandler[] helperHandlers)
        {
            if (memberLocator == null)
                memberLocator = MemberLocator.Default;

            var state = new HandlebarsParserState(templateId, memberLocator);
            var tokens = HandlebarsTokenizer.Tokenize(templateReader);
            state.BlockStack.PushNewBlockWithModel(modelType, new SourceLocation(templateId, 0, 0));

            var helpers = SyntaxHandlers.Union(GetHelperHandlers(helperHandlers ?? Enumerable.Empty<IHelperHandler>())).Union(SyntaxHandlersAfter).ToList();

            foreach (var token in tokens)
            {
                state.SetCurrentToken(token);

                foreach (var handler in helpers)
                {
                    if (handler.Key(token))
                    {
                        handler.Value.Invoke(state);
                        if (state.ContinueProcessingToken)
                        {
                            state.ContinueProcessingToken = false;
                            continue;
                        }
                        break;
                    }
                }
            }

            state.AssertStackOnRootNode();

            return state.RootNode;
        }
Ejemplo n.º 2
0
        public static HelperExpressionNode Helper(string[] parameter, IHelperHandler helperHandler, SourceLocation location)
        {
            var data = new Dictionary <string, string>();

            if (parameter.Length > 1)
            {
                foreach (var value in parameter.Skip(1))
                {
                    var tmp = value.Split(new[] { '=' }, 2);
                    data.Add(tmp[0], tmp.Length == 2 ? tmp[1] : string.Empty);
                }
            }

            return(new HelperExpressionNode
            {
                Location = location,
                Name = parameter[0],
                Parameters = data,
                HelperHandler = helperHandler
            });
        }
Ejemplo n.º 3
0
	    private static IDictionary<Func<HandlebarsToken, bool>, Action<HandlebarsParserState>> GetHelperHandlers(IHelperHandler helper)
	    {
		    var blockHelper = helper as IBlockHelperHandler;
		    if (blockHelper != null)
		    {
				return new Dictionary<Func<HandlebarsToken, bool>, Action<HandlebarsParserState>>
				{
					{x => x.Content.StartsWith("#") && helper.IsSupported(x.Content.Substring(1)), state => HandleHelperStart(state, blockHelper)},
					{x => x.Content.StartsWith("/") && helper.IsSupported(x.Content.Substring(1)), state => HandleHelperEnd(state, blockHelper)}
				};
		    }

		    return new Dictionary<Func<HandlebarsToken, bool>, Action<HandlebarsParserState>>
		    {
				{x => helper.IsSupported(x.Content), state => HandleHelper(state, helper)}
		    };
	    }
Ejemplo n.º 4
0
	    private static void HandleHelper(HandlebarsParserState state, IHelperHandler helper)
	    {
		    string expression = state.CurrentToken.Content;
			state.AddNodeToCurrentBlock(SyntaxTreeExpression.Helper(expression, helper, state.CurrentLocation));
	    }
Ejemplo n.º 5
0
        private static IDictionary <Func <HandlebarsToken, bool>, Action <HandlebarsParserState> > GetHelperHandlers(IHelperHandler helper)
        {
            var blockHelper = helper as IBlockHelperHandler;

            if (blockHelper != null)
            {
                return(new Dictionary <Func <HandlebarsToken, bool>, Action <HandlebarsParserState> >
                {
                    { x => x.Content.StartsWith("#") && helper.IsSupported(x.Content.Substring(1)), state => HandleHelperStart(state, blockHelper) },
                    { x => x.Content.StartsWith("/") && helper.IsSupported(x.Content.Substring(1)), state => HandleHelperEnd(state, blockHelper) }
                });
            }

            return(new Dictionary <Func <HandlebarsToken, bool>, Action <HandlebarsParserState> >
            {
                { x => helper.IsSupported(x.Content), state => HandleHelper(state, helper) }
            });
        }
Ejemplo n.º 6
0
        public static HelperExpressionNode Helper(string expression, IHelperHandler helperHandler, SourceLocation location)
        {
            var parts = expression.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            return(Helper(parts, helperHandler, location));
        }
Ejemplo n.º 7
0
        private static void HandleHelper(HandlebarsParserState state, IHelperHandler helper)
        {
            string expression = state.CurrentToken.Content;

            state.AddNodeToCurrentBlock(SyntaxTreeExpression.Helper(expression, helper, state.CurrentLocation));
        }
Ejemplo n.º 8
0
 public static void Evaluate(IHelperHandler handler, object model, RenderingContext renderingContext, HelperExpressionNode node)
 {
     handler.Evaluate(model, renderingContext, node.Parameters);
 }
        public static HelperExpressionNode Helper(string[] parameter, IHelperHandler helperHandler, SourceLocation location)
		{
			var data = new Dictionary<string, string>();

			if (parameter.Length > 1)
			{
				foreach (var value in parameter.Skip(1))
				{
					var tmp = value.Split(new[] {'='}, 2);
					data.Add(tmp[0], tmp.Length == 2 ? tmp[1] : string.Empty);
				}
			}

			return new HelperExpressionNode
			{
				Location = location,
				Name = parameter[0],
				Parameters = data,
                HelperHandler = helperHandler
			};
		}
Ejemplo n.º 10
0
		public static HelperExpressionNode Helper(string expression, IHelperHandler helperHandler, SourceLocation location)
		{
			var parts = expression.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			return Helper(parts, helperHandler, location);
		}
        public static void Evaluate(IHelperHandler handler, object model, RenderingContext renderingContext, HelperExpressionNode node)
        {
			handler.Evaluate(model, renderingContext, node.Parameters);
        }