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); }
public void GetPage(string fromVirtualPath, string virtualPath, out SharpPage page, out SharpCodePage codePage) { if (!TryGetPage(fromVirtualPath, virtualPath, out page, out codePage)) { throw new FileNotFoundException($"Page at path was not found: '{virtualPath}'"); } }
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)); }
public bool TryGetPage(string fromVirtualPath, string virtualPath, out SharpPage page, out SharpCodePage codePage) { var pathMapKey = nameof(GetPage) + ">" + fromVirtualPath; var mappedPath = GetPathMapping(pathMapKey, virtualPath); if (mappedPath != null) { var mappedPage = Pages.GetPage(mappedPath); if (mappedPage != null) { page = mappedPage; codePage = null; return(true); } RemovePathMapping(pathMapKey, mappedPath); } var tryExactMatch = virtualPath.IndexOf('/') >= 0; //if nested path specified, look for an exact match first if (tryExactMatch) { var cp = GetCodePage(virtualPath); if (cp != null) { codePage = cp; page = null; return(true); } var p = Pages.GetPage(virtualPath); if (p != null) { page = p; codePage = null; return(true); } } //otherwise find closest match from page.VirtualPath var parentPath = fromVirtualPath.IndexOf('/') >= 0 ? fromVirtualPath.LastLeftPart('/') : ""; do { var seekPath = parentPath.CombineWith(virtualPath); var cp = GetCodePage(seekPath); if (cp != null) { codePage = cp; page = null; return(true); } var p = Pages.GetPage(seekPath); if (p != null) { page = p; codePage = null; SetPathMapping(pathMapKey, virtualPath, seekPath); return(true); } if (parentPath == "") { break; } parentPath = parentPath.IndexOf('/') >= 0 ? parentPath.LastLeftPart('/') : ""; } while (true); page = null; codePage = null; return(false); }
public SharpPage DefaultResolveLayout(SharpPage page) { page.Args.TryGetValue(SharpPages.Layout, out object layout); return(page.Context.Pages.ResolveLayoutPage(page, layout as string)); }
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); }
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 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); }
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); }
public static async Task WritePageAsync(this ScriptScopeContext scope, SharpPage page, SharpCodePage codePage, Dictionary <string, object> pageParams, CancellationToken token = default(CancellationToken)) { await scope.PageResult.WritePageAsync(page, codePage, scope.ScopeWithParams(pageParams), token); }
public override Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token) { // block.Argument key is unique to exact memory fragment, not string equality var invokerCtx = (Tuple <string, StaticMethodInvoker>)scope.Context.CacheMemory.GetOrAdd(block.Argument, key => { 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 = 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 MethodInvoker LazyInvoker = (instance, 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] = 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--; } };