Skip to content

Commit 5acff17

Browse files
committed
Feature: Auto-generate a TOC for the readme.
1 parent 2910b5f commit 5acff17

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

ShaderShrinker/UnitTests/ReadmeBuilder.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
// </summary>
1010
// -----------------------------------------------------------------------
1111

12+
using System;
13+
using System.Collections.Generic;
1214
using System.IO;
1315
using System.Linq;
1416
using System.Reflection;
1517
using NUnit.Framework;
18+
using Shrinker.Lexer;
1619

1720
namespace UnitTests
1821
{
@@ -32,17 +35,22 @@ public void BuildTheReadme()
3235
var readmeLines = File.ReadAllLines(readmeFile.FullName).TakeWhile(o => !o.StartsWith("# Features")).ToList();
3336
readmeLines.Add("# Features");
3437

38+
var tocIndex = readmeLines.Count;
39+
var toc = new List<string>();
40+
3541
// Find options XAML.
3642
var optionsXamlFile = rootDir.EnumerateFiles("OptionsDialog.xaml", SearchOption.AllDirectories).FirstOrDefault();
3743
Assert.That(optionsXamlFile, Is.Not.Null);
3844

3945
// Read all the Markdown tooltip content.
4046
var inTooltip = false;
47+
var indent = 0;
4148
foreach (var xamlLine in File.ReadAllLines(optionsXamlFile.FullName))
4249
{
4350
if (xamlLine.Contains("<MdXaml:MarkdownScrollViewer"))
4451
{
4552
inTooltip = true;
53+
indent = xamlLine.IndexOf('<') + 4;
4654
}
4755
else if (xamlLine.Contains("</MdXaml:MarkdownScrollViewer"))
4856
{
@@ -52,10 +60,33 @@ public void BuildTheReadme()
5260
}
5361

5462
if (inTooltip && !xamlLine.TrimStart().StartsWith("<"))
55-
readmeLines.Add(xamlLine.Trim());
63+
{
64+
var mdLine = xamlLine
65+
.Substring(Math.Min(xamlLine.Length, indent))
66+
.TrimEnd()
67+
.Replace("```c", "```glsl");
68+
69+
if (xamlLine.TrimStart().StartsWith("### "))
70+
{
71+
// Build 'features' TOC.
72+
var heading = xamlLine.Trim(' ', '#');
73+
toc.Add($"* [{heading}](#{ToHeaderLink(heading)})");
74+
75+
mdLine = mdLine.Replace("### ", "## "); // Elevate the header level one-above what we use in the XAML.
76+
}
77+
78+
readmeLines.Add(mdLine);
79+
}
5680
}
5781

82+
readmeLines.InsertRange(tocIndex, toc);
5883
File.WriteAllLines(readmeFile.FullName, readmeLines);
5984
}
85+
86+
private static string ToHeaderLink(string s)
87+
{
88+
s = s.Replace(' ', '-');
89+
return s.ToLower();
90+
}
6091
}
6192
}

0 commit comments

Comments
 (0)