Skip to content

Commit

Permalink
SP5 Cookbook: show source code on recipe pages
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Nov 11, 2022
1 parent 341d39e commit d692ea2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/www
/dev/www/cookbook
src/ScottPlot4/ScottPlot.Cookbook/CookbookOutput

## Ignore Visual Studio temporary files, build results, and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ public void Generate_Cookbook_Pages()
HtmlFrontPage frontPage = new();
frontPage.Generate();

List<RecipeSource> sources = SourceReading.GetRecipeSources();

foreach (Info.PageInfo page in Query.GetPages())
{
HtmlRecipePage recipePage = new(page);
HtmlRecipePage recipePage = new(page, sources);
recipePage.Generate();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/ScottPlot5/ScottPlot5 Cookbook/HtmlPages/HtmlRecipePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ internal class HtmlRecipePage : HtmlPageBase
{
private readonly PageInfo Page;

public HtmlRecipePage(PageInfo page)
public HtmlRecipePage(PageInfo page, List<RecipeSource> sources)
{
Page = page;

foreach (RecipeInfo recipe in Page.Recipes)
recipe.AddSource(sources);
}

public void Generate()
Expand All @@ -30,10 +33,7 @@ public void Generate()
{
SB.AppendLine($"<h3>{recipe.Name}</h3>");
SB.AppendLine($"<p>{recipe.Description}</p>");
if (!string.IsNullOrEmpty(recipe.SourceCode))
{
SB.AppendLine($"<pre>{recipe.SourceCode}</pre>");
}
SB.AppendLine($"<pre>{recipe.SourceCode}</pre>");

SB.AppendLine($"<a href='{recipe.ImageFilename}'><img src='{recipe.ImageFilename}' /></a>");
}
Expand Down
16 changes: 15 additions & 1 deletion src/ScottPlot5/ScottPlot5 Cookbook/Info/RecipeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<RecipeSource> sources)
{
foreach (RecipeSource source in sources)
{
if (source.PageName == PageName && source.RecipeName == Name)
{
SourceCode = source.SourceCode;
break;
}
}
}
}
14 changes: 9 additions & 5 deletions src/ScottPlot5/ScottPlot5 Cookbook/SourceReading.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using FluentAssertions;
using ScottPlotCookbook.Info;
using System.Text;

namespace ScottPlotCookbook;

internal static class SourceReading
{
public static List<string> GetRecipeSourceFilePaths()
private static List<string> GetRecipeSourceFilePaths()
{
List<string> paths = new();

Expand Down Expand Up @@ -45,13 +46,13 @@ public static List<RecipeSource> 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;
}

Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit d692ea2

Please sign in to comment.