Beispiel #1
0
 private Template(JinjaEnvironment environment, ExpressionData templateNode, string?templateName, string?templatePath)
 {
     TemplateNode = templateNode;
     Environment  = environment;
     TemplateName = templateName;
     TemplatePath = templatePath;
 }
Beispiel #2
0
        internal override bool TryGetSource(JinjaEnvironment environment, string templateName, [NotNullWhen(true)] out TemplateInfo?templateInfo)
        {
            templateInfo = default;
            var upToDate = false;
            var path     = FindFile(templateName);

            if (path == null)
            {
                return(false);
            }
            var templateContents = File.ReadAllText(path.FullName, Encoding);

            if (_TemplateContentsCache.ContainsKey(templateName))
            {
                if (_TemplateContentsCache[templateName] == templateContents)
                {
                    upToDate = true;
                }
                else
                {
                    _TemplateContentsCache[templateName] = templateContents;
                }
            }
            else
            {
                _TemplateContentsCache.Add(templateName, templateContents);
            }
            templateInfo = new TemplateInfo(templateContents, path.FullName, upToDate);
            return(true);
        }
Beispiel #3
0
        internal static Template FromBlockNode(string templateName, JinjaEnvironment environment, BlockNode blockNode, IDictionary <string, object?> variableTemplate)
        {
            var rootScope = Scope.CreateRootScope("GLOBALS", variableTemplate);
            var expr      = NewASTCompiler.ToExpression(templateName, environment, blockNode, out var newcompiler, rootScope);

            return(new Template(environment, ExpressionData.CreateCompiled(expr, rootScope), blockNode.Name, null));
        }
Beispiel #4
0
 internal TemplateInfo GetSource(JinjaEnvironment environment, string templateName)
 {
     if (TryGetSource(environment, templateName, out var templateInfo) == false || templateInfo == null)
     {
         throw new TemplateNotFoundException(templateName);
     }
     return(templateInfo.Value);
 }
Beispiel #5
0
        //internal static Template LoadTemplate(JinjaEnvironment environment, string templateText, IDictionary<string, object?> variableTemplate, string? templateName, string? templatePath)
        //{
        //    var expr = ToExpression(environment, templateText, variableTemplate, out var compiler);
        //    return new Template(environment, compiler.Compile(expr), templateName, templatePath);
        //}
        internal static Template LoadTemplate(JinjaEnvironment environment, string templateText, IScope scope, string?templateName, string?templatePath)
        {
            var expr = ToExpression(templateName, environment, templateText, scope);

            var debug = expr.ToString("C#");
            var test  = new VariableSetterWalker();
            var x     = test.Visit(expr);

            ;

            return(new Template(environment, ExpressionData.CreateCompiled(expr, scope), templateName, templatePath));
        }
Beispiel #6
0
        internal static Template LoadTemplate(JinjaEnvironment environment, string templateText, IDictionary <string, object?> variableTemplate, string?templateName, string?templatePath)
        {
            var rootScope = Scope.CreateRootScope("GLOBALS", variableTemplate);
            var expr      = ToExpression(templateName, environment, templateText, rootScope);

            var debug = expr.ToString("C#");
            var test  = new VariableSetterWalker();
            var x     = test.Visit(expr);

            ;

            return(new Template(environment, ExpressionData.CreateCompiled(expr, rootScope), templateName, templatePath));
        }
Beispiel #7
0
        public virtual ITemplate Load(JinjaEnvironment environment, string name, IDictionary <string, object?> variableTemplate)
        {
            environment      = environment ?? throw new ArgumentNullException(nameof(environment));
            name             = name ?? throw new ArgumentNullException(nameof(name));
            variableTemplate = variableTemplate ?? throw new ArgumentNullException(nameof(variableTemplate));
            var templateInfo = GetSource(environment, name);

            if (_TemplateCache.ContainsKey(name) == false || templateInfo.UpToDate == false)
            {
                var template = environment.GetTemplate(name, templateInfo, variableTemplate);
                _TemplateCache.Upsert(name, template);
            }
            return(_TemplateCache[name]);
        }
Beispiel #8
0
        //internal static Expression ToExpression(JinjaEnvironment environment, string templateText, IDictionary<string, object?> variableTemplate, out ASTCompiler compiler)
        //{
        //    var lexer = new Lexer(environment);
        //    var tokens = lexer.Tokenize(templateText).ToArray();
        //    var parsed = Parser.Parse(tokens).ToArray();
        //    var environmentTrimmed = EnvironmentTrimming.EnvironmentTrim(parsed, environment.Settings).ToArray();
        //    var templateNode = ASTGenerator.ParseTemplate(environmentTrimmed);
        //    var commentsRemoved = templateNode.Transform(CommentRemoverTransformer.Instance);
        //    var controlledWhiteSpace = WhiteSpaceController.ControlWhiteSpace(commentsRemoved);
        //    var containerAssembled = controlledWhiteSpace.Transform(TemplateContainerAssembler.Instance);

        //    var finished = NewASTCompiler.ToExpression(environment, containerAssembled, variableTemplate, out var newcompiler);
        //    throw new NotImplementedException();
        //}
        internal static Expression ToExpression(string templateName, JinjaEnvironment environment, string templateText, IScope rootScope)
        {
            var lexer                = new Lexer(environment);
            var tokens               = lexer.Tokenize(templateText).ToArray();
            var parsed               = Parser.Parse(tokens).ToArray();
            var environmentTrimmed   = EnvironmentTrimming.EnvironmentTrim(parsed, environment.Settings).ToArray();
            var templateNode         = ASTGenerator.ParseTemplate(environmentTrimmed);
            var commentsRemoved      = templateNode.Transform(CommentRemoverTransformer.Instance);
            var controlledWhiteSpace = WhiteSpaceController.ControlWhiteSpace(commentsRemoved);
            var containerAssembled   = controlledWhiteSpace.Transform(TemplateContainerAssembler.Instance);

            var finished = NewASTCompiler.ToExpression(templateName, environment, containerAssembled, out var newcompiler, rootScope);

            return(finished);
        }
Beispiel #9
0
 internal abstract bool TryGetSource(JinjaEnvironment environment, string templateName, [NotNullWhen(true)] out TemplateInfo?templateInfo);