Ejemplo n.º 1
0
        public void Setup()
        {
            var blockFunctions      = new IBlockFunction[] { new ForEach(), new Literal(), new Strip(), new Capture() };
            var inlineFunctions     = new IInlineFunction[] { new LDelim(), new RDelim(), new Assign(), new Cycle() };
            var expressionFunctions = new IExpressionFunction[] { new If() };
            var variableModifiers   = new IVariableModifier[] { new Capitalize(), new Cat(), new CountCharacters(), new CountParagraphs(), new CountSentences(), new CountWords(), new DateFormat(), new Default(), new Lower(), new NewLineToBreak(), new RegexReplace(), new Replace(), new Spacify(), new StringFormat(), new ASmarty.VariableModifiers.Strip(), new StripTags(), new Truncate(), new Upper(), new WordWrap(), new Indent() };
            var functions           = new Functions(blockFunctions, inlineFunctions, expressionFunctions, variableModifiers);

            viewData     = new Dictionary <string, object>();
            functionData = new Dictionary <string, object>();

            var internalEvaluator = new InternalEvaluator(null, null, functions);

            evaluator         = new ViewEngine.Evaluator(internalEvaluator, 0);
            functionEvaluator = new FunctionEvaluator(internalEvaluator, 0, functionData);
        }
Ejemplo n.º 2
0
        public override string Evaluate()
        {
            string tmp = base.Evaluate();

            if (tmp.IndexOf(Separator) >= 0)
            {
                string[] split = tmp.Split(new char[] { Separator });
                if (split.Length != 2)
                {
                    throw new JcShellFormatException($"Unsupported format, ';' character should be included only once: {tmp}!");
                }

                var tmpData = split[0].Trim();
                var data    = _resolver(tmpData);
                if (data == null)
                {
                    throw new JcShellFormatException($"Unable to resolve the key value: {tmpData}!");
                }

                string format = split[1].Trim();
                foreach (var md in modifiers)
                {
                    IVariableModifier vm = md.CreateInstance(format);
                    if (vm != null)
                    {
                        return(vm.Transform(data));
                    }
                }
                throw new JcShellFormatException($"Unsupported format: {format}!");
            }

            var ret = _resolver(tmp);

            if (ret == null)
            {
                throw new JcShellFormatException($"Variable not found in the dictionary: {tmp}!");
            }

            return(ret);
        }