Exemplo n.º 1
0
        public void Visit(CaseWhenElseBlockTag caseWhenElseBlockTag)
        {
            var valueToMatchResult = LiquidExpressionEvaluator.Eval(caseWhenElseBlockTag.LiquidExpressionTree, _templateContext);

            //Console.WriteLine("Value to Match: "+valueToMatch);
            if (valueToMatchResult.IsError)
            {
                RegisterRenderingError(valueToMatchResult.ErrorResult);
                //RenderError(valueToMatchResult.ErrorResult);
                return;
            }

            var match =
                caseWhenElseBlockTag.WhenClauses.FirstOrDefault(
                    expr =>
                    // Take the valueToMatch "Case" expression result value
                    // and check if it's equal to the expr.GroupNameExpressionTree expression.
                    // THe "EasyValueComparer" is supposed to match stuff fairly liberally,
                    // though it doesn't cast values---probably it should.

                    expr.LiquidExpressionTree.Any(val =>
                                                  new EasyOptionComparer().Equals(valueToMatchResult.SuccessResult,
                                                                                  LiquidExpressionEvaluator.Eval(val, _templateContext).SuccessResult)));


            if (match != null)                   // found match
            {
                StartWalking(match.LiquidBlock); // so eval + render the HTML
            }
            else if (caseWhenElseBlockTag.HasElseClause)
            {
                StartWalking(caseWhenElseBlockTag.ElseClause.LiquidBlock);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Side effect: state is managed in the _counters dictionary.
        /// </summary>
        /// <returns></returns>
        private String GetNextCycleText(String groupName, CycleTag cycleTag)
        {
            int currentIndex;
            // Create a like dictionary key entry to keep track of this declaration.  THis takes the variable
            // names (not the eval-ed variables) or literals and concatenates them together.
            var key = "cycle_" + groupName + "_" + String.Join("|", cycleTag.CycleList.Select(x => x.Data.Expression.ToString()));


            while (true)
            {
                currentIndex = _counters.GetOrAdd(key, 0);
                var newindex = (currentIndex + 1) % cycleTag.Length;

                // fails if updated concurrently by someone else.
                if (_counters.TryUpdate(key, newindex, currentIndex))
                {
                    break;
                }
            }

            String result         = "";
            var    currentElement = cycleTag.ElementAt(currentIndex);

            LiquidExpressionEvaluator.Eval(currentElement, _templateContext)
            .WhenSuccess(x => result = ValueCaster.RenderAsString(LiquidExpressionEvaluator.Eval(currentElement, _templateContext).SuccessResult.Value))
            .WhenError(err => result = FormatErrors(new List <LiquidError> {
                err
            }));

            return(result);
        }
Exemplo n.º 3
0
 public void Visit(AssignTag assignTag)
 {
     LiquidExpressionEvaluator.Eval(assignTag.LiquidExpressionTree, _templateContext)
     .WhenSuccess(x => _templateContext.SymbolTableStack.DefineGlobal(assignTag.VarName, x))
     .WhenError(err =>
     {
         RegisterRenderingError(err);
         //RenderError(err);
     });
 }
Exemplo n.º 4
0
        public void Visit(LiquidExpressionTree liquidExpressionTree)
        {
            var result = LiquidExpressionEvaluator.Eval(liquidExpressionTree, _templateContext);

            result.WhenSuccess(success => success.WhenSome(x => AppendTextToCurrentAccumulator(Render(x))))
            .WhenError(error =>
            {
                RegisterRenderingError(error);
                //RenderingErrors.Add(error);
                //RenderError(error);
            });
        }
Exemplo n.º 5
0
        public void Visit(IfThenElseBlockTag ifThenElseBlockTag)
        {
            // find the first place where the expression tree evaluates to true (i.e. which of the if/elsif/else clauses)
            // This ignores "eval" errors in clauses.

            var match = ifThenElseBlockTag.IfElseClauses.FirstOrDefault(
                expr => LiquidExpressionResultIsTrue(LiquidExpressionEvaluator.Eval(expr.LiquidExpressionTree, _templateContext)));

            if (match != null)
            {
                StartWalking(match.LiquidBlock); // then render the contents
            }
        }
Exemplo n.º 6
0
        public void Visit(CustomTag customTag)
        {
            //Console.WriteLine("Looking up Custom Tag " + customTag.TagName);
            var tagType = _templateContext.SymbolTableStack.LookupCustomTagRendererType(customTag.TagName);

            if (tagType != null)
            {
                AppendTextToCurrentAccumulator(RenderCustomTag(customTag, tagType));
                return;
            }

            //Console.WriteLine("Looking up Macro "+ customTag.TagName);
            var macroDescription = _templateContext.SymbolTableStack.LookupMacro(customTag.TagName);

            if (macroDescription != null)
            {
                //Console.WriteLine("...");
                var evalResults =
                    customTag.LiquidExpressionTrees.Select(x => LiquidExpressionEvaluator.Eval(x, _templateContext)).ToList();
                if (evalResults.Any(x => x.IsError))
                {
                    var errors = evalResults.Where(x => x.IsError).Select(x => x.ErrorResult).ToList();
                    foreach (LiquidError error in errors)
                    {
                        RegisterRenderingError(error);
                    }
                    //RenderErrors(errors);
                    return;
                }
                AppendTextToCurrentAccumulator(RenderMacro(macroDescription, evalResults.Select(x => x.SuccessResult)));
                return;
            }

            var err = new LiquidError {
                Message = "Liquid syntax error: Unknown tag '" + customTag.TagName + "'"
            };

            //RenderError(err);
            RegisterRenderingError(err);
        }
Exemplo n.º 7
0
        public void EvalExpressions(
            IEnumerable <TreeNode <LiquidExpression> > expressionTrees,
            Action <IEnumerable <Option <ILiquidValue> > > successAction = null,
            Action <IEnumerable <LiquidError> > failureAction            = null)
        {
            var evaledArgs = expressionTrees.Select(x => LiquidExpressionEvaluator.Eval(x, _templateContext)).ToList();

            if (evaledArgs.Any(x => x.IsError))
            {
                if (failureAction != null)
                {
                    failureAction(evaledArgs.Where(x => x.IsError).Select(x => x.ErrorResult));
                }
            }
            else
            {
                if (successAction != null)
                {
                    successAction(evaledArgs.Select(x => x.SuccessResult));
                }
            }
        }
Exemplo n.º 8
0
 public void Visit(CycleTag cycleTag)
 {
     if (cycleTag.GroupNameExpressionTree == null)
     {
         AppendTextToCurrentAccumulator(GetNextCycleText(groupName: null, cycleTag: cycleTag));
     }
     else
     {
         // figure out the group name if any, otherwise use null.
         LiquidExpressionEvaluator.Eval(cycleTag.GroupNameExpressionTree, _templateContext)
         .WhenSuccess(
             x =>
             AppendTextToCurrentAccumulator(
                 GetNextCycleText(
                     groupName: x.HasValue ? ValueCaster.RenderAsString(x.Value) : null,
                     cycleTag: cycleTag)))
         .WhenError(err => {
             RegisterRenderingError(err);
             //RenderError(err);
         });
     }
 }