Skip to content

Commit

Permalink
add toc excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jul 28, 2019
1 parent 8d111a5 commit 6ba7625
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
3 changes: 0 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ A [dotnet tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools)
* [Mark resulting files as read only](#mark-resulting-files-as-read-only)
* [Table of contents](#table-of-contents)
* [LinkFormat](#linkformat)
* [Release Notes](#release-notes)
* [Credits](#credits)
* [Icon](#icon)
<!-- endtoc -->


Expand Down
14 changes: 12 additions & 2 deletions src/MarkdownSnippets/Processing/DirectoryMarkdownProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class DirectoryMarkdownProcessor
DirectoryFilter directoryFilter;
bool readOnly;
int tocLevel;
IEnumerable<string> tocExcludes;
Action<string> log;
string targetDirectory;
List<string> sourceMdFiles = new List<string>();
Expand All @@ -29,12 +30,15 @@ public class DirectoryMarkdownProcessor
DirectoryFilter directoryFilter = null,
bool readOnly = true,
LinkFormat linkFormat = LinkFormat.GitHub,
int tocLevel = 2)
int tocLevel = 2,
IEnumerable<string> tocExcludes = null)
{
this.writeHeader = writeHeader;
this.directoryFilter = directoryFilter;
this.readOnly = readOnly;
this.tocLevel = tocLevel;
this.tocExcludes = tocExcludes;

if (appendSnippetGroup == null)
{
this.appendSnippetGroup = new SnippetMarkdownHandling(targetDirectory, linkFormat).AppendGroup;
Expand Down Expand Up @@ -123,7 +127,13 @@ public void Run()
{
Guard.AgainstNull(snippets, nameof(snippets));
Guard.AgainstNull(snippetSourceFiles, nameof(snippetSourceFiles));
var processor = new MarkdownProcessor(snippets.ToDictionary(), appendSnippetGroup, snippetSourceFiles, writeHeader, tocLevel);
var processor = new MarkdownProcessor(
snippets.ToDictionary(),
appendSnippetGroup,
snippetSourceFiles,
writeHeader,
tocLevel,
tocExcludes);
foreach (var sourceFile in sourceMdFiles)
{
ProcessFile(sourceFile, processor);
Expand Down
15 changes: 13 additions & 2 deletions src/MarkdownSnippets/Processing/MarkdownProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ public class MarkdownProcessor
AppendSnippetGroupToMarkdown appendSnippetGroup;
bool writeHeader;
int tocLevel;
List<string> tocExcludes;
List<string> snippetSourceFiles;

public MarkdownProcessor(
IReadOnlyDictionary<string, IReadOnlyList<Snippet>> snippets,
AppendSnippetGroupToMarkdown appendSnippetGroup,
IReadOnlyList<string> snippetSourceFiles,
bool writeHeader,
int tocLevel)
int tocLevel,
IEnumerable<string> tocExcludes = null)
{
Guard.AgainstNull(snippets, nameof(snippets));
Guard.AgainstNull(appendSnippetGroup, nameof(appendSnippetGroup));
Expand All @@ -32,6 +34,15 @@ public class MarkdownProcessor
this.appendSnippetGroup = appendSnippetGroup;
this.writeHeader = writeHeader;
this.tocLevel = tocLevel;
if (tocExcludes == null)
{
this.tocExcludes = new List<string>();
}
else
{
this.tocExcludes = tocExcludes.ToList();
}

InitSourceFiles(snippetSourceFiles);
}

Expand Down Expand Up @@ -132,7 +143,7 @@ void AppendLine(string s)

if (tocLine != null)
{
tocLine.Current = TocBuilder.BuildToc(headerLines, tocLevel);
tocLine.Current = TocBuilder.BuildToc(headerLines, tocLevel, tocExcludes);
}

return new ProcessResult(
Expand Down
14 changes: 11 additions & 3 deletions src/MarkdownSnippets/Processing/TocBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

static class TocBuilder
{
public static string BuildToc(List<Line> headerLines, int level)
public static string BuildToc(List<Line> headerLines, int level, List<string> tocExcludes)
{
var processed = new List<string>();
var builder = new StringBuilder(@"<!-- toc -->
Expand All @@ -22,28 +22,36 @@ public static string BuildToc(List<Line> headerLines, int level)
{
continue;
}

var headerLevel = current.Length - trimmedHash.Length;
if (headerLevel == 1)
{
continue;
}

if (headerLevel > headerDepth)
{
continue;
}

var title = current.Substring(3).Trim();
if (tocExcludes.Contains(title))
{
continue;
}

headingCount++;

var title = current.Substring(3).Trim();
var link = BuildLink(processed, title);
var indent = new string(' ', (headerLevel-1)*2);
var indent = new string(' ', (headerLevel - 1) * 2);
builder.AppendLine($"{indent}* [{title}](#{link})");
}

if (headingCount == 0)
{
return string.Empty;
}

builder.AppendLine("<!-- endtoc -->");
return builder.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -18,7 +19,13 @@ public void Run()
targetDirectory: root,
scanForMdFiles: false,
scanForSnippets: false,
tocLevel: 1);
tocLevel: 1,
tocExcludes: new List<string>
{
"Icon",
"Credits",
"Release Notes"
});
processor.IncludeMdFiles(Path.Combine(root, "readme.source.md"));
var doc = Path.Combine(root, "docs");
var files = Directory.EnumerateFiles(doc, "*.source.md", SearchOption.AllDirectories).ToArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- toc -->
## Contents

* [Heading1](#heading1)
<!-- endtoc -->
27 changes: 20 additions & 7 deletions src/Tests/MarkdownProcessor/TocBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void EmptyHeading()
new Line("##", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines,1));
Approvals.Verify(TocBuilder.BuildToc(lines, 1, new List<string>()));
}

[Fact]
Expand All @@ -26,7 +26,19 @@ public void IgnoreTop()
new Line("## Heading2", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines,1));
Approvals.Verify(TocBuilder.BuildToc(lines, 1, new List<string>()));
}

[Fact]
public void Exclude()
{
var lines = new List<Line>
{
new Line("## Heading1", "", 0),
new Line("### Heading2", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines, 1, new List<string> {"Heading2"}));
}

[Fact]
Expand All @@ -40,8 +52,9 @@ public void Nested()
new Line("### Heading4", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines,2));
Approvals.Verify(TocBuilder.BuildToc(lines, 2, new List<string>()));
}

[Fact]
public void StopAtLevel()
{
Expand All @@ -52,7 +65,7 @@ public void StopAtLevel()
new Line("#### Heading3", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines,2));
Approvals.Verify(TocBuilder.BuildToc(lines, 2, new List<string>()));
}

[Fact]
Expand All @@ -63,7 +76,7 @@ public void Single()
new Line("## Heading", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines,1));
Approvals.Verify(TocBuilder.BuildToc(lines, 1, new List<string>()));
}

[Fact]
Expand All @@ -74,7 +87,7 @@ public void WithSpaces()
new Line("## A B ", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines,1));
Approvals.Verify(TocBuilder.BuildToc(lines, 1, new List<string>()));
}

[Fact]
Expand All @@ -87,7 +100,7 @@ public void Duplicates()
new Line("## a", "", 0)
};

Approvals.Verify(TocBuilder.BuildToc(lines,1));
Approvals.Verify(TocBuilder.BuildToc(lines, 1, new List<string>()));
}

public TocBuilderTests(ITestOutputHelper output) :
Expand Down

0 comments on commit 6ba7625

Please sign in to comment.