Ejemplo n.º 1
0
        public object Resolve(
            string expression,
            IDictionary <string, object> model)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            if (!Accept(expression))
            {
                throw new Exception($"Invalid equality expression: `{ expression }`.");
            }

            var match = Regex.Match(expression, regexPattern);

            var leftExp  = match.Groups["left"].Value;
            var rightExp = match.Groups["right"].Value;

            var engine = new RazorAlikeEngine();

            var left  = engine.ResolveExpression(leftExp, model);
            var right = engine.ResolveExpression(rightExp, model);

            return(left.Equals(right));
        }
        public (StringBuilder, StringReader) Execute(
            string currentLine,
            StringReader templateReader,
            StringBuilder output,
            IDictionary <string, object> model = null)
        {
            if (currentLine == null)
            {
                throw new ArgumentNullException(nameof(currentLine));
            }

            var forEachDetails = Regex.Match(currentLine, @"@foreach\s*\((?'criteria'.*)\)");

            if (!forEachDetails.Success)
            {
                throw new MalformedException($"Malformed foreach statement: `{ currentLine }`.");
            }

            var loopCriteria = forEachDetails.Groups["criteria"].Value.Trim();

            if (!loopCriteria.StartsWith("var"))
            {
                throw new MalformedException("Missing keyword `var`");
            }

            var variableRefs = Regex.Match(loopCriteria, @"var\s*(?'varName'[\w]+)\s*in\s*(?'collection'[\w.]+)");

            if (!variableRefs.Success)
            {
                throw new MalformedException(loopCriteria);
            }

            var varName            = variableRefs.Groups["varName"].Value;
            var collectionNavProps = variableRefs.Groups["collection"].Value;

            // resolve collection
            if (!(model.GetReference(collectionNavProps) is ICollection collection))
            {
                throw new Exception($"`{ collectionNavProps }` isn't a valid collection");
            }

            // look for the loop statements.
            var forBlock = BlockReader.ReadBlock(templateReader).ReadToEnd();

            var engine = new RazorAlikeEngine();

            foreach (var item in collection)
            {
                var iteratorModel = model.Clone();
                iteratorModel.DefineVariable(varName, item);

                var iterationOutput = engine.Run(forBlock, iteratorModel);

                output.Append(iterationOutput.ToString());
            }

            return(output, templateReader);
        }
Ejemplo n.º 3
0
        private bool EvaluateCondition(
            string condition,
            IDictionary <string, object> model)
        {
            if (condition == null)
            {
                throw new ArgumentNullException(nameof(condition));
            }

            // transformar este if num IStatement.
            if (condition.Trim().Equals("true", StringComparison.InvariantCultureIgnoreCase))
            {
                return(true);
            }

            if (condition.Trim().Equals("false", StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            var match = Regex.Match(condition, @"(?'left'[^=<>!]*)(?'operator'[=<>!]*)(?'right'[^=<>!]*)");

            if (match.Success)
            {
                var engine = new RazorAlikeEngine();

                var leftExp  = match.Groups["left"].Value.Trim();
                var rightExp = match.Groups["right"].Value.Trim();
                var op       = match.Groups["operator"].Value;

                var leftObj  = engine.ResolveExpression(leftExp, model);
                var rightObj = !string.IsNullOrEmpty(rightExp) ? engine.ResolveExpression(rightExp, model) : null;

                switch (op)
                {
                case null:
                case "":
                    if (!(leftObj is bool))
                    {
                        throw new InvalidCastException($"Expression `{ leftExp }` isn't a boolean variable.");
                    }
                    return((bool)leftObj);

                case "==":
                    return(leftObj.Equals(rightObj));

                case "!=":
                    return(!leftObj.Equals(rightObj));

                default:
                    throw new NotImplementedException($"Operator not implemented: `{ op }`.");
                }
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 4
0
        private void EvaluateStatements(
            string statements,
            IDictionary <string, object> model,
            StringBuilder output)
        {
            var engine = new RazorAlikeEngine();
            var result = engine.Run(statements, model);

            output.AppendLine(result);
        }
        public (StringBuilder, StringReader) Execute(
            string currentLine,
            StringReader templateReader,
            StringBuilder output,
            IDictionary <string, object> model = null)
        {
            var blockBody = BlockReader.ReadBlock(new StringReader(currentLine));

            var engine   = new RazorAlikeEngine();
            var resolved = engine.Run(blockBody.ReadToEnd(), model);

            output.AppendLine(resolved);

            return(output, templateReader);
        }
        public void SimplePositiveCase()
        {
            var template = @"
Hello @user.name!

@if (@user.showId == true) { Your ID is: @user.id. }

Mail addresses: 
@foreach(var email in user.Emails) 
{
    @email.Value
}
";
            var model    = new {
                user = new {
                    name   = "Joseph Climber",
                    id     = 12345,
                    showId = true,
                    Emails = new List <object>()
                    {
                        new { Value = "*****@*****.**" },
                        new { Value = "*****@*****.**" },
                    }
                }
            };

            var engine = new RazorAlikeEngine();
            var result = engine.Run(template, model);

            Assert.AreEqual(
                @"
Hello Joseph Climber!

Your ID is: 12345.

Mail addresses: 

    [email protected]
    [email protected]", result);
        }