コード例 #1
0
ファイル: ScriptMethods.cs プロジェクト: YAFNET/YAFNET
    /// <summary>
    /// Tries the get page.
    /// </summary>
    /// <param name="scope">The scope.</param>
    /// <param name="virtualPath">The virtual path.</param>
    /// <param name="page">The page.</param>
    /// <param name="codePage">The code page.</param>
    /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
    public static bool TryGetPage(this ScriptScopeContext scope, string virtualPath, out SharpPage page, out SharpCodePage codePage)
    {
        if (scope.PageResult.Partials.TryGetValue(virtualPath, out page))
        {
            codePage = null;
            return(true);
        }

        if (!scope.Context.TryGetPage(scope.PageResult.VirtualPath, virtualPath, out page, out codePage))
        {
            return(false);
        }

        codePage?.Init();

        if (codePage is IRequiresRequest requiresRequest)
        {
            if (scope.GetValue(ScriptConstants.Request) is IRequest request)
            {
                requiresRequest.Request = request;
            }
        }

        return(true);
    }
コード例 #2
0
ファイル: ScriptScopeContext.cs プロジェクト: YAFNET/YAFNET
    /// <summary>
    /// Creates the scoped context.
    /// </summary>
    /// <param name="scope">The scope.</param>
    /// <param name="template">The template.</param>
    /// <param name="scopeParams">The scope parameters.</param>
    /// <param name="cachePage">if set to <c>true</c> [cache page].</param>
    /// <returns>ScriptScopeContext.</returns>
    public static ScriptScopeContext CreateScopedContext(this ScriptScopeContext scope, string template, Dictionary <string, object> scopeParams = null, bool cachePage = true)
    {
        SharpPage dynamicPage = null;

        if (cachePage)
        {
            scope.Context.Cache.TryGetValue(template, out object value);
            dynamicPage = value as SharpPage;
        }

        if (dynamicPage == null)
        {
            dynamicPage = scope.Context.OneTimePage(template);

            if (cachePage)
            {
                scope.Context.Cache[template] = dynamicPage;
            }
        }

        var newScopeParams = new Dictionary <string, object>(scope.ScopedParams);

        scopeParams.Each((key, val) => newScopeParams[key] = val);

        var pageResult = scope.PageResult.Clone(dynamicPage).Init().Result;
        var itemScope  = new ScriptScopeContext(pageResult, scope.OutputStream, newScopeParams);

        return(itemScope);
    }
コード例 #3
0
ファイル: PageFormat.cs プロジェクト: YAFNET/YAFNET
    /// <summary>
    /// HTMLs the resolve layout.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <returns>SharpPage.</returns>
    public SharpPage HtmlResolveLayout(SharpPage page)
    {
        var isCompletePage = page.BodyContents.Span.StartsWithIgnoreCase("<!DOCTYPE HTML>".AsSpan()) || page.BodyContents.Span.StartsWithIgnoreCase("<html".AsSpan());

        if (isCompletePage)
        {
            return(null);
        }

        return(base.DefaultResolveLayout(page));
    }
コード例 #4
0
        public static PageResult ToPageResult(this PropertyValidatorContext context, SharpPage page)
        {
            var to = new PageResult(page)
            {
                Args =
                {
                    [ScriptConstants.It]    = context.PropertyValue,
                    [ScriptConstants.Field] = context.PropertyName,
                }
            };

            return(to);
        }
コード例 #5
0
 public ScriptConditionValidator(SharpPage code) : base(new LanguageStringSource(nameof(PredicateValidator)))
 {
     Code = code;
 }
コード例 #6
0
 public TemplateCodePageHandler(SharpCodePage page, SharpPage layoutPage = null)
 {
     this.page       = page;
     this.layoutPage = layoutPage;
 }
コード例 #7
0
 public TemplatePageHandler(SharpPage page, SharpPage layoutPage = null)
 {
     this.RequestName = !string.IsNullOrEmpty(page.VirtualPath) ? page.VirtualPath : nameof(TemplatePageHandler);
     this.Page        = page;
     this.LayoutPage  = layoutPage;
 }
コード例 #8
0
ファイル: ScriptMethods.cs プロジェクト: YAFNET/YAFNET
    /// <summary>
    /// Gets the parameters with item binding only.
    /// </summary>
    /// <param name="scope">The scope.</param>
    /// <param name="filterName">Name of the filter.</param>
    /// <param name="page">The page.</param>
    /// <param name="scopedParams">The scoped parameters.</param>
    /// <param name="itemBinding">The item binding.</param>
    /// <returns>Dictionary&lt;System.String, System.Object&gt;.</returns>
    /// <exception cref="System.NotSupportedException">'it' option in filter '{filterName}' should contain the name to bind to but contained a '{bindingName.GetType().Name}' instead</exception>
    public static Dictionary <string, object> GetParamsWithItemBindingOnly(this ScriptScopeContext scope, string filterName, SharpPage page, object scopedParams, out string itemBinding)
    {
        var pageParams = scope.AssertOptions(filterName, scopedParams);

        itemBinding = pageParams.TryGetValue("it", out object bindingName) && bindingName is string binding
                          ? binding
                          : "it";

        if (bindingName != null && bindingName is not string)
        {
            throw new NotSupportedException($"'it' option in filter '{filterName}' should contain the name to bind to but contained a '{bindingName.GetType().Name}' instead");
        }

        // page vars take precedence
        if (page != null && page.Args.TryGetValue("it", out object pageBinding))
        {
            itemBinding = (string)pageBinding;
        }

        return(pageParams);
    }
コード例 #9
0
ファイル: ScriptMethods.cs プロジェクト: YAFNET/YAFNET
    /// <summary>
    /// Gets the parameters with item binding.
    /// </summary>
    /// <param name="scope">The scope.</param>
    /// <param name="filterName">Name of the filter.</param>
    /// <param name="page">The page.</param>
    /// <param name="scopedParams">The scoped parameters.</param>
    /// <param name="itemBinding">The item binding.</param>
    /// <returns>Dictionary&lt;System.String, System.Object&gt;.</returns>
    public static Dictionary <string, object> GetParamsWithItemBinding(this ScriptScopeContext scope, string filterName, SharpPage page, object scopedParams, out string itemBinding)
    {
        var scopeParams = scope.GetParamsWithItemBindingOnly(filterName, page, scopedParams, out itemBinding);

        scopeParams.Each((key, val) => scope.ScopedParams[key] = val);
        return(scopeParams);
    }
コード例 #10
0
ファイル: PageFormat.cs プロジェクト: YAFNET/YAFNET
 /// <summary>
 /// Defaults the resolve layout.
 /// </summary>
 /// <param name="page">The page.</param>
 /// <returns>SharpPage.</returns>
 public SharpPage DefaultResolveLayout(SharpPage page)
 {
     page.Args.TryGetValue(SharpPages.Layout, out object layout);
     return(page.Context.Pages.ResolveLayoutPage(page, layout as string));
 }
コード例 #11
0
ファイル: FunctionScriptBlock.cs プロジェクト: YAFNET/YAFNET
    /// <summary>
    /// Writes the asynchronous.
    /// </summary>
    /// <param name="scope">The scope.</param>
    /// <param name="block">The block.</param>
    /// <param name="token">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <returns>Task.</returns>
    public override Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token)
    {
        // block.Argument key is unique to exact memory fragment, not string equality
        // Parse into AST once for all Page Results
        var invokerCtx = (Tuple <string, StaticMethodInvoker>)scope.Context.CacheMemory.GetOrAdd(block.Argument, _ =>
        {
            var literal = block.Argument.Span.ParseVarName(out var name);
            var strName = name.ToString();
            literal     = literal.AdvancePastWhitespace();

            literal  = literal.AdvancePastWhitespace();
            var args = TypeConstants.EmptyStringArray;
            if (!literal.IsEmpty)
            {
                literal.ParseArgumentsList(out var argIdentifiers);
                args = new string[argIdentifiers.Count];
                for (var i = 0; i < argIdentifiers.Count; i++)
                {
                    args[i] = argIdentifiers[i].Name;
                }
            }

            StaticMethodInvoker invoker = null;

            // Allow recursion by initializing lazy Delegate
            object LazyInvoker(object instance, object[] paramValues)
            {
                if (invoker == null)
                {
                    throw new NotSupportedException($"Uninitialized function '{strName}'");
                }

                return(invoker(instance, paramValues));
            }

            invoker = paramValues =>
            {
                scope.PageResult.StackDepth++;
                try
                {
                    var page       = new SharpPage(Context, block.Body);
                    var pageResult = new PageResult(page)
                    {
                        Args =
                        {
                            [strName] = (MethodInvoker)LazyInvoker
                        },
                        StackDepth = scope.PageResult.StackDepth
                    };

                    var len = Math.Min(paramValues.Length, args.Length);
                    for (int i = 0; i < len; i++)
                    {
                        var paramValue           = paramValues[i];
                        pageResult.Args[args[i]] = paramValue;
                    }

                    if (pageResult.EvaluateResult(out var returnValue))
                    {
                        return(returnValue);
                    }

                    return(IgnoreResult.Value);
                }
                finally
                {
                    scope.PageResult.StackDepth--;
                }
            };