public Response ProcessRequest(NancyContext context, string path) { path = string.Concat("~", path.Substring(PathPrefix.Length)); using (bundles.GetReadLock()) { Bundle bundle; IAsset asset; if (!bundles.TryGetAssetByPath(path, out asset, out bundle)) { logger.Info("ProcessRequest : Asset '{0}' not found", path); return new HtmlResponse(HttpStatusCode.NotFound); } var actualETag = "\"" + asset.Hash.ToHexString() + "\""; var givenETag = context.Request.Headers["If-None-Match"]; if(givenETag.Equals(actualETag)) { logger.Info("ProcessRequest : Asset '{0}' not modified", path); var notModified = new HtmlResponse(HttpStatusCode.NotModified); notModified.ContentType = bundle.ContentType; return notModified; } logger.Info("ProcessRequest : Asset '{0}' returned", path); var response = new StreamResponse(asset.OpenStream, bundle.ContentType); response.WithHeader("ETag", actualETag); return response; } }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var template = renderContext.ViewCache.GetOrAdd(viewLocationResult, result => { try { var context = new NancyVeilContext(renderContext, Extensions); var engine = new VeilEngine(context); Type modelType = model == null ? typeof(object) : model.GetType(); return engine.CompileNonGeneric(viewLocationResult.Extension, result.Contents(), modelType); } catch (Exception e) { return CreateErrorPage(e); } }); var response = new HtmlResponse(); response.ContentType = "text/html; charset=utf-8"; response.Contents = s => { var writer = new StreamWriter(s, Encoding.UTF8); template(writer, model); writer.Flush(); }; return response; }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var response = new HtmlResponse(); var html = renderContext.ViewCache.GetOrAdd(viewLocationResult, result => { string markDown = viewLocationResult.Contents() .ReadToEnd(); var parser = new Markdown(); return parser.Transform(markDown); }); var serverHtml = ParagraphSubstitution.Replace(html, "$1"); var renderHtml = this.engineWrapper.Render(serverHtml, model, new MarkdownViewEngineHost(new NancyViewEngineHost(renderContext), renderContext)); response.Contents = stream => { var writer = new StreamWriter(stream); writer.Write(renderHtml); writer.Flush(); }; return response; }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var response = new HtmlResponse(); var html = renderContext.ViewCache.GetOrAdd(viewLocationResult, result => ConvertMarkdown(viewLocationResult)); response.Contents = stream => { var writer = new StreamWriter(stream); writer.Write(html); writer.Flush(); }; return response; }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var response = new HtmlResponse(); string HTML = renderContext.ViewCache.GetOrAdd(viewLocationResult, result => { string markDown = viewLocationResult.Contents().ReadToEnd (); var parser = new MarkdownSharp.Markdown(); return parser.Transform(markDown); }); response.Contents = stream => { var writer = new StreamWriter(stream); writer.Write(HTML); writer.Flush(); }; return response; }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var response = new HtmlResponse(); var html = renderContext.ViewCache.GetOrAdd( viewLocationResult, result => { return ConvertMarkdown(viewLocationResult); }); var engineHost = new MarkdownViewEngineHost( new NancyViewEngineHost(renderContext), renderContext, Extensions); var renderHtml = engineWrapper.Render(html, model, engineHost); response.Contents = stream => { var writer = new StreamWriter(stream); writer.Write(renderHtml); writer.Flush(); }; return response; }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var viewFile = viewLocationResult.Location + "/" + viewLocationResult.Name + "." + viewLocationResult.Extension; var response = new HtmlResponse { Contents = stream => { var viewModel = model ?? new ExpandoObject(); var context = new VelocityContext(); context.Put("Model", viewModel); context.Put("Helper", new Helper()); var writer = new StreamWriter(stream); var template = velocityEngine.GetTemplate(viewFile); template.Merge(context, writer); writer.Flush(); } }; return response; }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var response = new HtmlResponse(); var html = renderContext.ViewCache.GetOrAdd(viewLocationResult, result => { string markDown = File.ReadAllText(rootPathProvider.GetRootPath() + viewLocationResult.Location + Path.DirectorySeparatorChar + viewLocationResult.Name + ".md"); MarkdownOptions options = new MarkdownOptions(); options.AutoNewLines = false; var parser = new Markdown(options); return parser.Transform(markDown); }); /* <p> - matches the literal string "<p>" ( - creates a capture group, so that we can get the text back by backreferencing in our replacement string @ - matches the literal string "@" [^<]* - matches any character other than the "<" character and does this any amount of times ) - ends the capture group </p> - matches the literal string "</p>" */ var regex = new Regex("<p>(@[^<]*)</p>"); var serverHtml = regex.Replace(html, "$1"); var renderHtml = this.engineWrapper.Render(serverHtml, model, new MarkdownViewEngineHost(new NancyViewEngineHost(renderContext), renderContext)); response.Contents = stream => { var writer = new StreamWriter(stream); writer.Write(renderHtml); writer.Flush(); }; return response; }
/// <summary> /// Renders the view. /// </summary> /// <param name="viewLocationResult">A <see cref="ViewLocationResult"/> instance, containing information on how to get the view template.</param> /// <param name="model">The model that should be passed into the view</param> /// <param name="renderContext">The render context.</param> /// <param name="isPartial">Used by HtmlHelpers to declare a view as partial</param> /// <returns>A response.</returns> public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext, bool isPartial) { Assembly referencingAssembly = null; if (model != null) { var underlyingSystemType = model.GetType().UnderlyingSystemType; if (underlyingSystemType != null) { referencingAssembly = Assembly.GetAssembly(underlyingSystemType); } } var response = new HtmlResponse(); response.Contents = stream => { var writer = new StreamWriter(stream); var view = this.GetViewInstance(viewLocationResult, renderContext, referencingAssembly, model); view.ExecuteView(null, null); var body = view.Body; var sectionContents = view.SectionContents; var layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial); var root = string.IsNullOrWhiteSpace(layout); while (!root) { view = this.GetViewInstance(renderContext.LocateView(layout, model), renderContext, referencingAssembly, model); view.ExecuteView(body, sectionContents); body = view.Body; sectionContents = view.SectionContents; layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial); root = !view.HasLayout; } writer.Write(body); writer.Flush(); }; return response; }
/// <summary> /// Renders the view. /// </summary> /// <param name="viewLocationResult">A <see cref="ViewLocationResult"/> instance, containing information on how to get the view template.</param> /// <param name="model">The model that should be passed into the view</param> /// <param name="renderContext">The render context.</param> /// <param name="isPartial">Used by HtmlHelpers to declare a view as partial</param> /// <returns>A response.</returns> public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext, bool isPartial) { Assembly referencingAssembly = null; var response = new HtmlResponse(); response.Contents = stream => { var writer = new StreamWriter(stream); var view = this.GetViewInstance(viewLocationResult, renderContext, model); view.ExecuteView(null, null); var body = view.Body; var sectionContents = view.SectionContents; var layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial); var root = string.IsNullOrWhiteSpace(layout); while (!root) { var viewLocation = renderContext.LocateView(layout, model); if (viewLocation == null) { throw new InvalidOperationException("Unable to locate layout: " + layout); } view = this.GetViewInstance(viewLocation, renderContext, model); view.ExecuteView(body, sectionContents); body = view.Body; sectionContents = view.SectionContents; layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial); root = !view.HasLayout; } writer.Write(body); writer.Flush(); }; return response; }
public Response Rewrite(NancyContext context) { if (!IsCassetteRequest(context)) return null; string path; if (!TryGetFilePath(context, out path)) return null; var file = settings.SourceDirectory.GetFile(path); // file.LastWriteTimeUtc var hash = fileContentHasher.Hash(path).ToHexString(); var actualETag = "\"" + hash + "\""; //if (request.PathInfo.Contains(hash)) //{ // SetFarFutureExpiresHeader(context.Response, actualETag); //} //else //{ // NoCache(context.Response); //} var givenETag = context.Request.Headers["If-None-Match"]; if (givenETag.Contains(actualETag)) { logger.Info("ProcessRequest : File '{0}' not modified", path); var notModified = new HtmlResponse(HttpStatusCode.NotModified); return notModified; } context.Request.Url.Path = "/" + path; return null; }