Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/NaiveStaticGenerator/NaiveStaticGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Markdig" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Yadg.NET\Yadg.NET\Yadg.NET.csproj" />
</ItemGroup>
Expand Down
64 changes: 41 additions & 23 deletions src/NaiveStaticGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using NaiveStaticGenerator;
using Markdig;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Renderers.Html.Inlines;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using NaiveStaticGenerator;
using System.Text;
using YadgNet;

Expand Down Expand Up @@ -89,33 +95,34 @@

static string MdToHtml(string md, string title)
{
//
// The wiki is written for GitHub, so it is read as GitHub reads it: markdown, with the HTML in it passed through
var pipeline = new MarkdownPipelineBuilder().UsePipeTables().UseEmphasisExtras().UseAutoLinks().UseTaskLists().Build();
var document = Markdown.Parse(md.Replace("\r", ""), pipeline);

// A link to another page of the wiki names the page, as `[Expressions](Expressions)` does, and the page is Expressions.html here
foreach (var link in document.Descendants<LinkInline>())
if (!link.IsImage && link.Url is { } url && IsWikiPage(url))
link.Url = url.IndexOf('#') is var hash and >= 0 ? url[..hash] + ".html" + url[hash..] : url + ".html";

// The site's highlight.js guesses each block's language, and it has no F#: a block marked as F# would not be highlighted at all
foreach (var block in document.Descendants<FencedCodeBlock>())
block.GetAttributes().Classes?.Clear();

var html = new StringWriter();
var renderer = new HtmlRenderer(html);
pipeline.Setup(renderer);
renderer.ObjectRenderers.Replace<CodeInlineRenderer>(new CodeInlineAsCw());
renderer.Render(document);

var sb = new StringBuilder();
sb.Append($"<h2 class='centered'>{title}</h2><hr>");
sb.Append("<p><a href='index.html'>&#8592; Back to the main page</a></p>");
sb.Append($"{md}");
sb.Replace("\r", "");
// sb.Replace("\n\n", "<br><br>");
sb.Append(html);
sb.Replace("<img ", "<img style='width: 100%'");
return
Wrap("`", "`", "<text class='cw'>", "</text>",
Wrap("```", "```", "<pre><code>", "</code></pre>",
Wrap("```cs", "```", "<pre><code>", "</code></pre>",
Wrap("```fs", "```", "<pre><code>", "</code></pre>",
sb.ToString()
)
)
)
);
return sb.ToString();

static string Wrap(string before, string after, string newBefore, string newAfter, string src)
{
var unjailed = DescriptionFromXmlBuilder.Unjail(before, after, src);
if (unjailed is not { } notNull)
return src;
var (inside, first, last) = notNull;
return src[..first] + newBefore + inside.Trim() + newAfter + Wrap(before, after, newBefore, newAfter, src[last..]);
}
static bool IsWikiPage(string url)
=> !url.Contains(':') && !url.StartsWith('/') && !url.StartsWith('#') && !Path.HasExtension(url.Split('#')[0]);
}
}

Expand Down Expand Up @@ -207,7 +214,7 @@
}


static string PathUp(string content, string relativeName, string subPath)

Check warning on line 217 in src/NaiveStaticGenerator/Program.cs

View workflow job for this annotation

GitHub Actions / RunJobs (macos-latest)

The local function 'PathUp' is declared but never used

Check warning on line 217 in src/NaiveStaticGenerator/Program.cs

View workflow job for this annotation

GitHub Actions / RunJobs (windows-latest)

The local function 'PathUp' is declared but never used

Check warning on line 217 in src/NaiveStaticGenerator/Program.cs

View workflow job for this annotation

GitHub Actions / RunJobs (ubuntu-latest)

The local function 'PathUp' is declared but never used
=> content.Replace("/" + subPath,
string.Join("",
Enumerable.Range(0, relativeName.Count(c => c == '\\')
Expand Down Expand Up @@ -254,7 +261,7 @@
}
}

void CopyWikiImagesToFinalWebsite()

Check warning on line 264 in src/NaiveStaticGenerator/Program.cs

View workflow job for this annotation

GitHub Actions / RunJobs (macos-latest)

The local function 'CopyWikiImagesToFinalWebsite' is declared but never used

Check warning on line 264 in src/NaiveStaticGenerator/Program.cs

View workflow job for this annotation

GitHub Actions / RunJobs (windows-latest)

The local function 'CopyWikiImagesToFinalWebsite' is declared but never used

Check warning on line 264 in src/NaiveStaticGenerator/Program.cs

View workflow job for this annotation

GitHub Actions / RunJobs (ubuntu-latest)

The local function 'CopyWikiImagesToFinalWebsite' is declared but never used
{
DirectoryCopy(RootP._("wiki"), FinalOutputP._("wiki"), copySubDirs: false, _ => true);
}
Expand Down Expand Up @@ -378,6 +385,17 @@
}
}

// Inline code in the wiki's pages, in the site's own style for it
public sealed class CodeInlineAsCw : HtmlObjectRenderer<CodeInline>
{
protected override void Write(HtmlRenderer renderer, CodeInline code)
{
renderer.Write("<text class='cw'>");
renderer.WriteEscape(code.ContentSpan);
renderer.Write("</text>");
}
}

public static class Extensions
{
public static string _(this string a, string b) => Path.Combine(a, b);
Expand Down
Loading