示例#1
0
        public LunetTemplateContext CreatePageContext()
        {
            var context = new LunetTemplateContext(Builtins);

            context.PushGlobal(Site.Builtins);
            return(context);
        }
示例#2
0
 public bool TryImportScriptStatement(string scriptStatement, IDynamicObject scriptObject, ScriptFlags flags, out object result, LunetTemplateContext context = null)
 {
     if (scriptStatement == null)
     {
         throw new ArgumentNullException(nameof(scriptStatement));
     }
     if (scriptObject == null)
     {
         throw new ArgumentNullException(nameof(scriptObject));
     }
     return(TryImportScript(scriptStatement, "__script__", scriptObject, flags, out result, context));
 }
示例#3
0
        public bool TryImportScriptFromFile(FileEntry scriptPath, IDynamicObject scriptObject, ScriptFlags flags, out object result, LunetTemplateContext context = null)
        {
            if (scriptPath == null)
            {
                throw new ArgumentNullException(nameof(scriptPath));
            }
            if (scriptObject == null)
            {
                throw new ArgumentNullException(nameof(scriptObject));
            }
            result = null;

            var scriptExist = scriptPath.Exists;

            if ((flags & ScriptFlags.Expect) != 0)
            {
                if (!scriptExist)
                {
                    Site.Error($"Config file [{scriptPath}] does not exist");
                    return(false);
                }
            }

            if (scriptExist)
            {
                var configAsText = scriptPath.ReadAllText();
                return(TryImportScript(configAsText, scriptPath.Path, scriptObject, flags, out result, context));
            }
            return(true);
        }
示例#4
0
        public bool TryImportScript(string scriptText, UPath scriptPath, IDynamicObject scriptObject, ScriptFlags flags, out object result, LunetTemplateContext context = null)
        {
            if (scriptText == null)
            {
                throw new ArgumentNullException(nameof(scriptText));
            }
            if (scriptPath == null)
            {
                throw new ArgumentNullException(nameof(scriptPath));
            }
            if (scriptObject == null)
            {
                throw new ArgumentNullException(nameof(scriptObject));
            }

            result = null;
            context ??= new LunetTemplateContext(Builtins);

            var scriptResult = ParseScript(scriptText, scriptPath.FullName, ScriptMode.ScriptOnly);

            if (!scriptResult.HasErrors)
            {
                if ((flags & ScriptFlags.AllowSiteFunctions) != 0)
                {
                    context.PushGlobal(SiteFunctions);
                }

                context.PushGlobal((ScriptObject)scriptObject);
                context.PushSourceFile(scriptPath.FullName);
                context.EnableOutput   = false;
                context.TemplateLoader = (flags & ScriptFlags.AllowSiteFunctions) != 0 ? unauthorizedTemplateLoader : new TemplateLoaderFromIncludes(Site);

                try
                {
                    result = scriptResult.Template.Evaluate(context);
                }
                catch (ScriptRuntimeException exception)
                {
                    LogException(exception);
                    return(false);
                }
                finally
                {
                    context.PopSourceFile();
                    context.PopGlobal();
                    if ((flags & ScriptFlags.AllowSiteFunctions) != 0)
                    {
                        context.PopGlobal();
                    }
                }
                return(true);
            }
            return(false);
        }