private static bool ExtensionMatchesView(string viewName, ViewLocationResult viewLocationResult) { var extension = Path.GetExtension(viewName); return string.IsNullOrEmpty(extension) || viewLocationResult.Extension.Equals(extension.Substring(1), StringComparison.OrdinalIgnoreCase); }
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; }
private static bool NameMatchesView(string viewName, ViewLocationResult viewLocationResult) { var name = Path.GetFileNameWithoutExtension(viewName); return (!string.IsNullOrEmpty(name)) && viewLocationResult.Name.Equals(name, StringComparison.OrdinalIgnoreCase); }
private static bool LocationMatchesView(string viewName, ViewLocationResult viewLocationResult) { var location = viewName .Replace(Path.GetFileName(viewName), string.Empty) .TrimEnd(new [] { '/' }); return viewLocationResult.Location.Equals(location, StringComparison.OrdinalIgnoreCase); }
/// <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> /// <returns>A delegate that can be invoked with the <see cref="Stream"/> that the view should be rendered to.</returns> public Action<Stream> RenderView(ViewLocationResult viewLocationResult, dynamic model) { return s => { var writer = new StreamWriter(s); writer.Write(this.viewEngine.Render(viewLocationResult.Contents.Invoke().ReadToEnd(), model)); writer.Flush(); }; }
private static bool LocationMatchesView(string viewName, ViewLocationResult viewLocationResult) { var filename = Path.GetFileName( viewName ); var index = viewName.LastIndexOf(filename, System.StringComparison.OrdinalIgnoreCase ); var location = index >= 0 ? viewName.Remove( index, filename.Length ) : viewName; location = location.TrimEnd( new[] { '/' } ); return viewLocationResult.Location.Equals(location, StringComparison.OrdinalIgnoreCase); }
private static Action<Stream> SafeInvokeViewEngine(IViewEngine viewEngine, ViewLocationResult locationResult, dynamic model) { try { return viewEngine.RenderView(locationResult, model); } catch (Exception) { return EmptyView; } }
public void Should_ignore_case_when_locating_view_based_on_name(string viewName) { // Given var expectedView = new ViewLocationResult(string.Empty, "index", string.Empty, () => null); var locator = CreateViewLocator(expectedView); // When var result = locator.LocateView(viewName, null); // Then result.ShouldBeSameAs(expectedView); }
public Action<Stream> RenderView(ViewLocationResult viewLocationResult, dynamic model) { return stream => { var transformer = new MarkdownSharp.Markdown(_options); string markdown = viewLocationResult.Contents.ReadToEnd(); string html = transformer.Transform(markdown); var writer = new StreamWriter(stream); writer.Write(html); writer.Flush(); }; }
public void Should_locate_view_when_only_name_is_provided() { // Given var expectedView = new ViewLocationResult(string.Empty, "index", string.Empty, () => null); var locator = CreateViewLocator(expectedView); // When var result = locator.LocateView("index", null); // Then result.ShouldBeSameAs(expectedView); }
public void Should_be_able_to_locate_view_by_name_when_the_viewname_occures_in_the_location() { // Given var expectedView = new ViewLocationResult( "views/hello", "hello", "cshtml", () => null ); //var additionalView = new ViewLocationResult( "views", "index", "spark", () => null ); var locator = CreateViewLocator(expectedView); // When var result = locator.LocateView( "views/hello/hello", null ); // Then result.ShouldBeSameAs( expectedView ); }
public void Should_be_able_to_locate_view_by_name_and_extension_when_two_view_with_same_name_but_different_extensions_exists_in_the_same_location() { // Given var expectedView = new ViewLocationResult("views", "index", "cshtml", () => null); var additionalView = new ViewLocationResult("views", "index", "spark", () => null); var locator = CreateViewLocator(expectedView, additionalView); // When var result = locator.LocateView("views/index.cshtml", null); // Then result.ShouldBeSameAs(expectedView); }
public void Should_be_able_to_locate_view_by_name_when_two_views_with_same_name_exists_at_different_locations() { // Given var expectedView = new ViewLocationResult("views/sub", "index", string.Empty, () => null); var additionalView = new ViewLocationResult("views", "index", string.Empty, () => null); var locator = CreateViewLocator(expectedView, additionalView); // When var result = locator.LocateView("views/sub/index", null); // Then result.ShouldBeSameAs(expectedView); }
public ViewEngineFixture() { this.templateLocator = A.Fake<IViewLocator>(); this.viewCompiler = A.Fake<IViewCompiler>(); this.view = A.Fake<IView>(); this.viewLocationResult = new ViewLocationResult(@"c:\some\fake\path", null); A.CallTo(() => templateLocator.GetTemplateContents("test")).Returns(viewLocationResult); A.CallTo(() => viewCompiler.GetCompiledView<object>(null)).Returns(view); A.CallTo(() => viewCompiler.GetCompiledView<MemoryStream>(null)).Returns(view); this.engine = new ViewEngine(templateLocator, viewCompiler); }
public void Should_throw_ambiguousviewsexception_when_locating_view_by_name_returns_multiple_results() { // Given var expectedView1 = new ViewLocationResult(string.Empty, "index", string.Empty, () => null); var expectedView2 = new ViewLocationResult(string.Empty, "index", string.Empty, () => null); var locator = CreateViewLocator(expectedView1, expectedView2); // When var exception = Record.Exception(() => locator.LocateView("index", null)); // Then exception.ShouldBeOfType<AmbiguousViewsException>(); }
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) { foreach (var engine in this.viewEngines.Where(x => x != this)) { if (engine.Extensions.Contains(viewLocationResult.Extension)) { var timer = renderContext.Context.GetTimer(); var result = timer.Time(() => engine.RenderView(viewLocationResult, model, renderContext)); renderContext.Context.GetMessageBroker().Publish(new Message { Id = Guid.NewGuid() } .AsTimedMessage(result) .AsTimelineMessage("Render View", new TimelineCategoryItem("Views", "#999", "#bbb")) ); return result.Result; } } return null; }
public string ConvertMarkdown(ViewLocationResult viewLocationResult) { var content = viewLocationResult.Contents().ReadToEnd(); // Parse out the post settings var startOfSettingsIndex = content.IndexOf("---", StringComparison.InvariantCultureIgnoreCase); if (startOfSettingsIndex >= 0) { var endOfSettingsIndex = content.IndexOf( "---", startOfSettingsIndex + 3, StringComparison.InvariantCultureIgnoreCase); endOfSettingsIndex += 3; content = content.Substring(endOfSettingsIndex, content.Length - endOfSettingsIndex); } return new Markdown().Transform(content); }
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 void Should_ignore_case_when_locating_view_engine_for_view_name_extension() { // Given var viewEngines = new[] { A.Fake<IViewEngine>(), }; A.CallTo(() => viewEngines[0].Extensions).Returns(new[] { "HTML" }); var location = new ViewLocationResult("location", "name", "html", GetEmptyContentReader()); A.CallTo(() => this.resolver.GetViewLocation(A<string>.Ignored, A<object>.Ignored, A<ViewLocationContext>.Ignored)).Returns(location); var factory = this.CreateFactory(viewEngines); // When factory.RenderView("foo", null, new ViewLocationContext()); // Then A.CallTo(() => viewEngines[0].RenderView(location, null)).MustHaveHappened(); }
public void Should_get_render_context_from_factory_when_rendering_view() { // Given var viewEngines = new[] { A.Fake<IViewEngine>(), }; A.CallTo(() => viewEngines[0].Extensions).Returns(new[] { "html" }); var location = new ViewLocationResult("location", "name", "html", GetEmptyContentReader()); A.CallTo(() => this.resolver.GetViewLocation(A<string>.Ignored, A<object>.Ignored, A<ViewLocationContext>.Ignored)).Returns(location); var factory = this.CreateFactory(viewEngines); // When factory.RenderView("view.html", new object(), this.viewLocationContext); // Then A.CallTo(() => this.renderContextFactory.GetRenderContext(A<ViewLocationContext>.Ignored)).MustHaveHappened(); }
public void Should_covert_anonymoustype_model_to_expandoobject_before_invoking_view_engine() { // Given var viewEngines = new[] { A.Fake<IViewEngine>(), }; A.CallTo(() => viewEngines[0].Extensions).Returns(new[] { "html" }); var location = new ViewLocationResult("location", "name", "html", GetEmptyContentReader()); A.CallTo(() => this.resolver.GetViewLocation(A<string>.Ignored, A<object>.Ignored, A<ViewLocationContext>.Ignored)).Returns(location); var model = new { Name = "" }; var factory = this.CreateFactory(viewEngines); // When factory.RenderView("foo", model, this.viewLocationContext); // Then A.CallTo(() => viewEngines[0].RenderView(A<ViewLocationResult>.Ignored, A<object>.That.Matches(x => x.GetType().Equals(typeof(ExpandoObject))), A<IRenderContext>.Ignored)).MustHaveHappened(); }
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 void Should_call_first_view_engine_that_supports_extension_with_view_location_results() { // Given var viewEngines = new[] { A.Fake<IViewEngine>(), A.Fake<IViewEngine>(), }; A.CallTo(() => viewEngines[0].Extensions).Returns(new[] { "html" }); A.CallTo(() => viewEngines[1].Extensions).Returns(new[] { "html" }); var location = new ViewLocationResult(string.Empty, "html", null); A.CallTo(() => this.locator.GetViewLocation("foo", A<IEnumerable<string>>.Ignored.Argument)).Returns(location); var factory = this.CreateFactory(viewEngines); // When var action = factory["foo"]; // Then A.CallTo(() => viewEngines[0].RenderView(location, null)).MustHaveHappened(); }
public void Should_call_first_view_engine_that_supports_extension_with_view_location_results() { // Given var viewEngines = new[] { A.Fake<IViewEngine>(), A.Fake<IViewEngine>(), }; A.CallTo(() => viewEngines[0].Extensions).Returns(new[] { "html" }); A.CallTo(() => viewEngines[1].Extensions).Returns(new[] { "html" }); var location = new ViewLocationResult("location", "name", "html", GetEmptyContentReader()); A.CallTo(() => this.resolver.GetViewLocation(A<string>.Ignored, A<object>.Ignored, A<ViewLocationContext>.Ignored)).Returns(location); var factory = this.CreateFactory(viewEngines); // When factory.RenderView("foo", null, this.viewLocationContext); // Then A.CallTo(() => viewEngines[0].RenderView(location, null, A<IRenderContext>.Ignored)).MustHaveHappened(); }
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; }
public void return_viewlocationresult_when_view_could_be_located() { // Given const string viewName = "foo.html"; var resolver = new DefaultViewResolver( this.viewLocator, new ViewLocationConventions(new Func<string, dynamic, ViewLocationContext, string>[] { (name, model, path) => "bar.html" })); var locatedView = new ViewLocationResult("name", "location", "extension", GetEmptyContentReader()); A.CallTo(() => this.viewLocator.LocateView(A<string>.Ignored)).Returns(locatedView); // When var result = resolver.GetViewLocation(viewName, null, this.viewLocationContext); // Then result.ShouldBeSameAs(locatedView); }
public void Should_return_view_location_result_from_source_provider_when_view_could_be_found() { // Given var viewSourceProvider = A.Fake<IViewSourceProvider>(); var viewLocationResult = new ViewLocationResult(null, string.Empty, null); var locator = new DefaultViewLocator(new[] { viewSourceProvider }); A.CallTo(() => viewSourceProvider.LocateView(A<string>.Ignored, A<IEnumerable<string>>.Ignored)).Returns(viewLocationResult); // When var result = locator.GetViewLocation("view name", new[] { "html" }); // Then result.ShouldBeSameAs(viewLocationResult); }
public void Should_ignore_case_when_locating_view_engine_for_view_name_extension() { // Given var viewEngines = new[] { A.Fake<IViewEngine>(), }; A.CallTo(() => viewEngines[0].Extensions).Returns(new[] { "HTML" }); var location = new ViewLocationResult(string.Empty, "html", null); A.CallTo(() => this.locator.GetViewLocation("foo", A<IEnumerable<string>>.Ignored.Argument)).Returns(location); var factory = this.CreateFactory(viewEngines); // When var action = factory["foo"]; // Then A.CallTo(() => viewEngines[0].RenderView(location, null)).MustHaveHappened(); }
private static string GetFullLocationOfView(ViewLocationResult viewLocationResult) { return(string.Concat(viewLocationResult.Location, "/", viewLocationResult.Name, ".", viewLocationResult.Extension)); }
private static bool LocationMatchesView(string viewName, ViewLocationResult viewLocationResult) { var location = GetLocationFromViewName(viewName); return(viewLocationResult.Location.Equals(location, StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Gets or adds a view from the cache. /// </summary> /// <typeparam name="TCompiledView">The type of the cached view instance.</typeparam> /// <param name="viewLocationResult">A <see cref="ViewLocationResult"/> instance that describes the view that is being added or retrieved from the cache.</param> /// <param name="valueFactory">A function that produces the value that should be added to the cache in case it does not already exist.</param> /// <returns>An instance of the type specified by the <typeparamref name="TCompiledView"/> type.</returns> public TCompiledView GetOrAdd <TCompiledView>(ViewLocationResult viewLocationResult, Func <ViewLocationResult, TCompiledView> valueFactory) { return((TCompiledView)this.cache.GetOrAdd(viewLocationResult, (x) => valueFactory(x))); }