Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont write path when doing explicit file include #312

Merged
merged 10 commits into from
Oct 5, 2020
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
3 changes: 2 additions & 1 deletion src/ConfigReader.Tests/ConfigReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public Task Empty()
[Fact]
public Task BadJson()
{
return Verifier.Throws(()=>ConfigReader.Parse(@"{
return Verifier.Throws(
() => ConfigReader.Parse(@"{
""ValidateContent"": true
""Convention"": ""InPlaceOverwrite""
}"));
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigReader/ConfigDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static ConfigResult Convert(ConfigInput? fileConfig, ConfigInput otherCon
DocumentExtensions = otherConfig.DocumentExtensions,
TocLevel = otherConfig.TocLevel.GetValueOrDefault(2),
MaxWidth = otherConfig.MaxWidth.GetValueOrDefault(int.MaxValue),
TreatMissingAsWarning = otherConfig.TreatMissingAsWarning.GetValueOrDefault()
TreatMissingAsWarning = otherConfig.TreatMissingAsWarning.GetValueOrDefault(),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/ConfigReader/LogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static string BuildConfigLogMessage(string root, ConfigResult config, str
TocLevel: {config.TocLevel}
MaxWidth: {config.MaxWidth}
ValidateContent: {config.ValidateContent}
TreatMissingAsWarning: {config.TreatMissingAsWarning}
FileConfigPath: {configFilePath} (exists:{File.Exists(configFilePath)})
");

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591</NoWarn>
<Version>21.1.4</Version>
<Version>21.2.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Markdown, Snippets, mdsnippets, documentation, MarkdownSnippets</PackageTags>
<Description>Extracts snippets from code files and merges them into markdown documents.</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/MarkdownSnippets.MsBuild/DocoTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override bool Execute()
MaxWidth = MaxWidth,
UrlsAsSnippets = UrlsAsSnippets,
DocumentExtensions = DocumentExtensions,
TreatMissingAsWarning = TreatMissingAsWarning
TreatMissingAsWarning = TreatMissingAsWarning,
});

var message = LogBuilder.BuildConfigLogMessage(root, configResult, configFilePath);
Expand Down
4 changes: 3 additions & 1 deletion src/MarkdownSnippets.Tool/CommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public static Task RunCommand(Invoke invoke, params string[] args)
Exclude = options.Exclude.ToList(),
TocExcludes = options.TocExcludes.ToList(),
DocumentExtensions = options.DocumentExtensions.ToList(),
UrlsAsSnippets = options.UrlsAsSnippets.ToList()
UrlsAsSnippets = options.UrlsAsSnippets.ToList(),
TreatMissingAsWarning = options.TreatMissingAsWarning,
Convention = options.Convention
};
return invoke(options.TargetDirectory!, configInput);
});
Expand Down
5 changes: 5 additions & 0 deletions src/MarkdownSnippets.Tool/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class Options
HelpText = "Write a header at the top of each resultant md file. Optional. Defaults to true")]
public bool? WriteHeader { get; set; }

[Option("missing-as-warning",
Required = false,
HelpText = "The default behavior for a missing snippet/include is to log an error (or throw an exception). To change that behavior to a warning set TreatMissingAsWarning to true. Optional. Defaults to false")]
public bool? TreatMissingAsWarning { get; set; }

[Option("header",
Required = false,
HelpText = @"The header to write. `{relativePath}` is replaced with the current .source.md file. Optional. Defaults to:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ public void Run()
targetDirectory,
validateContent,
header: header,
tocExcludes: tocExcludes, newLine: newLine);
tocExcludes: tocExcludes,
newLine: newLine);
foreach (var sourceFile in mdFiles)
{
ProcessFile(sourceFile, processor);
Expand Down
87 changes: 67 additions & 20 deletions src/MarkdownSnippets/Processing/IncludeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,30 @@ public bool TryProcessInclude(List<Line> lines, Line line, List<Include> used, i
return false;
}

static string GetIncludeKey(string substring)
{
var indexOfDotPath = substring.IndexOf(". path:");
if (indexOfDotPath != -1)
{
return substring.Substring(0, indexOfDotPath);
}

return substring.Substring(0, substring.Length - 4);
}

var indexSingleLineInclude = current.IndexOf("<!-- singleLineInclude: ", StringComparison.Ordinal);
if (indexSingleLineInclude > 0)
{
var substring = current.Substring(indexSingleLineInclude + 24);
var includeKey = substring.Substring(0, substring.IndexOf(". "));
var includeKey = GetIncludeKey(substring);
Inner(lines, line, used, index, missing, includeKey, relativePath);
return true;
}

if (current.StartsWith("<!-- include: ", StringComparison.Ordinal))
{
var substring = current.Substring(14);
var includeKey = substring.Substring(0, substring.IndexOf(". "));
var includeKey = GetIncludeKey(substring);
lines.RemoveUntil(index + 1, "<!-- endInclude -->", line.Path, line);
Inner(lines, line, used, index, missing, includeKey, relativePath);
return true;
Expand All @@ -62,7 +73,7 @@ public bool TryProcessInclude(List<Line> lines, Line line, List<Include> used, i
if (indexOfInclude > 0)
{
var substring = current.Substring(indexOfInclude + 14);
var includeKey = substring.Substring(0, substring.IndexOf(". "));
var includeKey = GetIncludeKey(substring);
lines.RemoveUntil(index + 1, "<!-- endInclude -->", line.Path, line);
Inner(lines, line, used, index, missing, includeKey, relativePath);
return true;
Expand All @@ -76,7 +87,7 @@ void Inner(List<Line> lines, Line line, List<Include> used, int index, List<Miss
var include = includes.SingleOrDefault(x => string.Equals(x.Key, includeKey, StringComparison.OrdinalIgnoreCase));
if (include != null)
{
AddInclude(lines, line, used, index, include);
AddInclude(lines, line, used, index, include, true);
return;
}

Expand All @@ -86,26 +97,26 @@ void Inner(List<Line> lines, Line line, List<Include> used, int index, List<Miss
if (success)
{
include = Include.Build(includeKey, File.ReadAllLines(httpPath!), null);
AddInclude(lines, line, used, index, include);
AddInclude(lines, line, used, index, include, false);
return;
}
}

if (RelativeFile.Find(allFiles, rootDirectory, includeKey, relativePath, line.Path, out var path))
{
include = Include.Build(includeKey, File.ReadAllLines(path!), path);
AddInclude(lines, line, used, index, include);
AddInclude(lines, line, used, index, include, false);
return;
}

missing.Add(new MissingInclude(includeKey, index + 1, line.Path));
line.Current = $"** Could not find include '{includeKey}' ** <!-- singleLineInclude: {includeKey} -->";
}

void AddInclude(List<Line> lines, Line line, List<Include> used, int index, Include include)
void AddInclude(List<Line> lines, Line line, List<Include> used, int index, Include include, bool writePath)
{
used.Add(include);
var linesToInject = BuildIncludes(line, include).ToList();
var linesToInject = BuildIncludes(line, include, writePath).ToList();
var first = linesToInject.First();
lines[index] = first;

Expand All @@ -116,37 +127,53 @@ void AddInclude(List<Line> lines, Line line, List<Include> used, int index, Incl
}
}

IEnumerable<Line> BuildIncludes(Line line, Include include)
IEnumerable<Line> BuildIncludes(Line line, Include include, bool writePath)
{
var path = GetPath(include);

var count = include.Lines.Count;
if (count == 0)
{
return BuildEmpty(line, path, include);
return BuildEmpty(line, path, include, writePath);
}

if (count == 1)
{
return BuildSingle(line, path, include);
return BuildSingle(line, path, include, writePath);
}

return BuildMultiple(line, path, include);
return BuildMultiple(line, path, include, writePath);
}

static IEnumerable<Line> BuildMultiple(Line line, string? path, Include include)
static IEnumerable<Line> BuildMultiple(Line line, string? path, Include include, bool writePath)
{
var count = include.Lines.Count;
var first = include.Lines.First();
var key = include.Key;
if (IsSnippetLineOrEndsWithTicks(first))
{
yield return line.WithCurrent($"<!-- include: {key}. path: {path} -->");
if (writePath)
{
yield return line.WithCurrent($"<!-- include: {key}. path: {path} -->");
}
else
{
yield return line.WithCurrent($"<!-- include: {key} -->");
}

yield return new Line(first, path, 1);
}
else
{
yield return line.WithCurrent($"{first} <!-- include: {key}. path: {path} -->");
if (writePath)
{
yield return line.WithCurrent($"{first} <!-- include: {key}. path: {path} -->");
}
else
{
yield return line.WithCurrent($"{first} <!-- include: {key} -->");
}

}

for (var index = 1; index < include.Lines.Count - 1; index++)
Expand Down Expand Up @@ -174,24 +201,44 @@ static bool IsSnippetLineOrEndsWithTicks(string line)
line.EndsWith("```");
}

static IEnumerable<Line> BuildEmpty(Line line, string? path, Include include)
static IEnumerable<Line> BuildEmpty(Line line, string? path, Include include, bool writePath)
{
yield return line.WithCurrent($"<!-- emptyInclude: {include.Key}. path: {path} -->");
if (writePath)
{
yield return line.WithCurrent($"<!-- emptyInclude: {include.Key}. path: {path} -->");
}

yield return line.WithCurrent($"<!-- emptyInclude: {include.Key} -->");
}

static IEnumerable<Line> BuildSingle(Line line, string? path, Include include)
static IEnumerable<Line> BuildSingle(Line line, string? path, Include include, bool writePath)
{
var first = include.Lines.First();
var key = include.Key;
if (IsSnippetLineOrEndsWithTicks(first))
{
yield return line.WithCurrent($"<!-- include: {key}. path: {path} -->");
if (writePath)
{
yield return line.WithCurrent($"<!-- include: {key}. path: {path} -->");
}
else
{
yield return line.WithCurrent($"<!-- include: {key} -->");
}

yield return new Line(first, path, 1);
yield return new Line("<!-- endInclude -->", path, 1);
}
else
{
yield return line.WithCurrent($"{first} <!-- singleLineInclude: {key}. path: {path} -->");
if (writePath)
{
yield return line.WithCurrent($"{first} <!-- singleLineInclude: {key}. path: {path} -->");
}
else
{
yield return line.WithCurrent($"{first} <!-- singleLineInclude: {key} -->");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
targetDirectory: 'CurrentDirectory',
configInput: {
Convention: 'InPlaceOverwrite'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
targetDirectory: 'CurrentDirectory',
configInput: {
Convention: 'InPlaceOverwrite'
}
}
14 changes: 14 additions & 0 deletions src/Tests/Console/CommandRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public async Task ValidateContentLong()
await VerifyResult();
}

[Fact]
public async Task ConventionShort()
{
await CommandRunner.RunCommand(Capture, "-c", "InPlaceOverwrite");
await VerifyResult();
}

[Fact]
public async Task ConventionLong()
{
await CommandRunner.RunCommand(Capture, "--convention", "InPlaceOverwrite");
await VerifyResult();
}

[Fact]
public async Task ReadOnlyShort()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
The include text 1 <!-- singleLineInclude: fileToInclude1.txt. path: /fileToInclude1.txt -->
The include text 1 <!-- singleLineInclude: fileToInclude1.txt -->

The include text 2 <!-- singleLineInclude: /fileToInclude2.txt. path: /fileToInclude2.txt -->
The include text 2 <!-- singleLineInclude: /fileToInclude2.txt -->

The include text 3 <!-- singleLineInclude: fileToInclude3.txt. path: /Nested/fileToInclude3.txt -->
The include text 3 <!-- singleLineInclude: fileToInclude3.txt -->

The include text 4 <!-- singleLineInclude: /fileToInclude4.txt. path: /Nested/fileToInclude4.txt -->
The include text 4 <!-- singleLineInclude: /fileToInclude4.txt -->

The include text 5 <!-- singleLineInclude: Nested/fileToInclude5.txt. path: /Nested/fileToInclude5.txt -->
The include text 5 <!-- singleLineInclude: Nested/fileToInclude5.txt -->

The include text 6 <!-- singleLineInclude: /Nested/fileToInclude6.txt. path: /Nested/fileToInclude6.txt -->
The include text 6 <!-- singleLineInclude: /Nested/fileToInclude6.txt -->
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
some content

The include text <!-- include: fileToInclude.txt. path: /fileToInclude.txt -->
The include text <!-- include: fileToInclude.txt -->

<!-- snippet: snippet1 -->
<a id='snippet-snippet1'></a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The include text <!-- include: fileToInclude.txt. path: /fileToInclude.txt -->
The include text <!-- include: fileToInclude.txt -->

<!-- snippet: snippet1 -->
<a id='snippet-snippet1'></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ the code from snippet1
<sup><a href='#snippet-snippet1' title='Navigate to start of snippet `snippet1`'>anchor</a></sup>
<!-- endSnippet -->

The include text <!-- singleLineInclude: fileToInclude.txt. path: /fileToInclude.txt -->
The include text <!-- singleLineInclude: fileToInclude.txt -->

Line 1 <!-- include: multiLineFileToInclude.txt. path: /multiLineFileToInclude.txt -->
Line 1 <!-- include: multiLineFileToInclude.txt -->

Line 2 <!-- endInclude -->

<!-- include: includeWithCode.txt. path: /includeWithCode.txt -->
<!-- include: includeWithCode.txt -->
```
The Code
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ the code from snippet1
<sup><a href='#snippet-snippet1' title='Navigate to start of snippet `snippet1`'>anchor</a></sup>
<!-- endSnippet -->

The include text <!-- singleLineInclude: fileToInclude.txt. path: /fileToInclude.txt -->
The include text <!-- singleLineInclude: fileToInclude.txt -->

Line 1 <!-- include: multiLineFileToInclude.txt. path: /multiLineFileToInclude.txt -->
Line 1 <!-- include: multiLineFileToInclude.txt -->

Line 2 <!-- endInclude -->

<!-- include: includeWithCode.txt. path: /includeWithCode.txt -->
<!-- include: includeWithCode.txt -->
```
The Code
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The MIT License (MIT) <!-- include: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt. path: -->
The MIT License (MIT) <!-- include: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt -->

Copyright (c) 2013 Simon Cropp

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The include text <!-- singleLineInclude: fIletoinClude.txt. path: /fileToInclude.txt -->
The include text <!-- singleLineInclude: fIletoinClude.txt -->
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The MIT License (MIT) <!-- include: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt. path: -->
The MIT License (MIT) <!-- include: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt -->

Copyright (c) 2013 Simon Cropp

Expand Down
Loading