From d692ea2d273db52fd5c07351a7903a913cd13140 Mon Sep 17 00:00:00 2001 From: Scott W Harden Date: Fri, 11 Nov 2022 00:36:01 -0500 Subject: [PATCH] SP5 Cookbook: show source code on recipe pages --- .gitignore | 2 +- .../HtmlPages/HtmlPageGenerator.cs | 4 +++- .../HtmlPages/HtmlRecipePage.cs | 10 +++++----- .../ScottPlot5 Cookbook/Info/RecipeInfo.cs | 16 +++++++++++++++- .../ScottPlot5 Cookbook/SourceReading.cs | 14 +++++++++----- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 53d34dcffc..e142eba7ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/www +/dev/www/cookbook src/ScottPlot4/ScottPlot.Cookbook/CookbookOutput ## Ignore Visual Studio temporary files, build results, and diff --git a/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlPageGenerator.cs b/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlPageGenerator.cs index 72340efe4e..997dbebffe 100644 --- a/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlPageGenerator.cs +++ b/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlPageGenerator.cs @@ -11,9 +11,11 @@ public void Generate_Cookbook_Pages() HtmlFrontPage frontPage = new(); frontPage.Generate(); + List sources = SourceReading.GetRecipeSources(); + foreach (Info.PageInfo page in Query.GetPages()) { - HtmlRecipePage recipePage = new(page); + HtmlRecipePage recipePage = new(page, sources); recipePage.Generate(); } } diff --git a/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlRecipePage.cs b/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlRecipePage.cs index 2d7b147860..c58f12ea0d 100644 --- a/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlRecipePage.cs +++ b/src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlRecipePage.cs @@ -6,9 +6,12 @@ internal class HtmlRecipePage : HtmlPageBase { private readonly PageInfo Page; - public HtmlRecipePage(PageInfo page) + public HtmlRecipePage(PageInfo page, List sources) { Page = page; + + foreach (RecipeInfo recipe in Page.Recipes) + recipe.AddSource(sources); } public void Generate() @@ -30,10 +33,7 @@ public void Generate() { SB.AppendLine($"

{recipe.Name}

"); SB.AppendLine($"

{recipe.Description}

"); - if (!string.IsNullOrEmpty(recipe.SourceCode)) - { - SB.AppendLine($"
{recipe.SourceCode}
"); - } + SB.AppendLine($"
{recipe.SourceCode}
"); SB.AppendLine($""); } diff --git a/src/ScottPlot5/ScottPlot5 Cookbook/Info/RecipeInfo.cs b/src/ScottPlot5/ScottPlot5 Cookbook/Info/RecipeInfo.cs index 850e3c9a19..54edb3ddc4 100644 --- a/src/ScottPlot5/ScottPlot5 Cookbook/Info/RecipeInfo.cs +++ b/src/ScottPlot5/ScottPlot5 Cookbook/Info/RecipeInfo.cs @@ -3,10 +3,11 @@ public class RecipeInfo { public string Name { get; } + public string PageName { get; } public string Description { get; } public string ImageFilename { get; } public string AnchorName { get; } - public string SourceCode { get; } = string.Empty; + public string SourceCode { get; private set; } = string.Empty; public IRecipe Recipe { get; } internal RecipeInfo(IRecipe recipe, PageInfo page) @@ -16,5 +17,18 @@ internal RecipeInfo(IRecipe recipe, PageInfo page) ImageFilename = $"{UrlTools.UrlSafe(Name)}.png"; AnchorName = $"#{UrlTools.UrlSafe(Name)}"; Recipe = recipe; + PageName = page.Name; + } + + internal void AddSource(List sources) + { + foreach (RecipeSource source in sources) + { + if (source.PageName == PageName && source.RecipeName == Name) + { + SourceCode = source.SourceCode; + break; + } + } } } diff --git a/src/ScottPlot5/ScottPlot5 Cookbook/SourceReading.cs b/src/ScottPlot5/ScottPlot5 Cookbook/SourceReading.cs index 9ca66a5606..b0a353f6f5 100644 --- a/src/ScottPlot5/ScottPlot5 Cookbook/SourceReading.cs +++ b/src/ScottPlot5/ScottPlot5 Cookbook/SourceReading.cs @@ -1,11 +1,12 @@ using FluentAssertions; +using ScottPlotCookbook.Info; using System.Text; namespace ScottPlotCookbook; internal static class SourceReading { - public static List GetRecipeSourceFilePaths() + private static List GetRecipeSourceFilePaths() { List paths = new(); @@ -45,13 +46,13 @@ public static List GetRecipeSources() { if (line.StartsWith(" PageName = ")) { - pageNameSafe = UrlTools.UrlSafe(line.Split('"')[1]); + pageNameSafe = line.Split('"')[1]; continue; } if (line.StartsWith(" public override string Name =")) { - recipeNameSafe = UrlTools.UrlSafe(line.Split('"')[1]); + recipeNameSafe = line.Split('"')[1]; continue; } @@ -87,7 +88,10 @@ public static void Test_Recipe_Sources_Found() sources.Should().NotBeEmpty(); sources.Should().HaveCount(Cookbook.GetRecipes().Count); - foreach (RecipeSource source in sources) - TestContext.WriteLine($"{source.PageName}/{source.RecipeName} ({source.SourceCode.Length} characters)"); + foreach (RecipeInfo recipe in Query.GetRecipes()) + { + recipe.AddSource(sources); + recipe.SourceCode.Should().NotBeNull(); + } } }