ymal array support
This commit is contained in:
parent
db93fe01d4
commit
e2029aa82e
@ -236,10 +236,81 @@ internal static partial class HelperMarkdown
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<string> GetFrontMatterLines(string[] parsedLines)
|
||||
{
|
||||
List<string> results = new();
|
||||
string afterTrim;
|
||||
string[] segments;
|
||||
StringBuilder stringBuilder = new();
|
||||
for (int i = 0; i < parsedLines.Length; i++)
|
||||
{
|
||||
afterTrim = parsedLines[i].Trim();
|
||||
if (string.IsNullOrEmpty(afterTrim) || afterTrim.First() is '{' or '}')
|
||||
continue;
|
||||
segments = afterTrim.Split(": ");
|
||||
if (segments.Length != 2)
|
||||
{
|
||||
if (results.Last()[^1] == '[')
|
||||
{
|
||||
_ = stringBuilder.Clear();
|
||||
_ = stringBuilder.Append(results.Last());
|
||||
results.RemoveAt(results.Count - 1);
|
||||
for (int j = i; j < parsedLines.Length; j++)
|
||||
{
|
||||
i = j;
|
||||
afterTrim = parsedLines[j].Trim();
|
||||
if (afterTrim != "],")
|
||||
_ = stringBuilder.Append(afterTrim);
|
||||
else
|
||||
_ = stringBuilder.Append(afterTrim[..^1]);
|
||||
if (afterTrim is "]" or "],")
|
||||
{
|
||||
results.Add(stringBuilder.ToString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
results.Clear();
|
||||
break;
|
||||
}
|
||||
if (afterTrim[^1] != ',')
|
||||
results.Add(afterTrim[1..].Replace("\": ", ": "));
|
||||
else
|
||||
results.Add(afterTrim[1..^1].Replace("\": ", ": "));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static (string?, Dictionary<string, JsonElement>?, List<string>) Get(List<string> jsonLines)
|
||||
{
|
||||
string? result;
|
||||
List<string> results;
|
||||
Dictionary<string, JsonElement>? keyValuePairs;
|
||||
string jsonLinesLast = jsonLines.Last();
|
||||
jsonLines.RemoveAt(jsonLines.Count - 1);
|
||||
jsonLines.Add(jsonLinesLast[..^1]);
|
||||
jsonLines.Insert(0, "{");
|
||||
jsonLines.Add("}");
|
||||
result = string.Join(Environment.NewLine, jsonLines);
|
||||
keyValuePairs = JsonSerializer.Deserialize(result, DictionaryStringAndJsonElementSourceGenerationContext.Default.DictionaryStringJsonElement);
|
||||
if (keyValuePairs is null)
|
||||
throw new NullReferenceException(nameof(keyValuePairs));
|
||||
result = JsonSerializer.Serialize(keyValuePairs, DictionaryStringAndJsonElementSourceGenerationContext.Default.DictionaryStringJsonElement);
|
||||
string[] parsedLines = result.Split(Environment.NewLine).ToArray();
|
||||
results = GetFrontMatterLines(parsedLines);
|
||||
if (!results.Any())
|
||||
{
|
||||
result = null;
|
||||
keyValuePairs = null;
|
||||
}
|
||||
return (result, keyValuePairs, results);
|
||||
}
|
||||
|
||||
private static (string?, Dictionary<string, JsonElement>?, string[]) Get(int frontMatterYamlEnd, string[] lines)
|
||||
{
|
||||
string? result;
|
||||
List<string> results = new();
|
||||
List<string> results;
|
||||
Dictionary<string, JsonElement>? keyValuePairs;
|
||||
string[] segments;
|
||||
string[] segmentsB;
|
||||
@ -268,6 +339,8 @@ internal static partial class HelperMarkdown
|
||||
jsonLines.Add($"\"{segmentsFirst}\": ");
|
||||
if (segmentsLast == "[]")
|
||||
jsonLines.RemoveAt(jsonLines.Count - 1);
|
||||
else if (segmentsLast.Length > 4 && segmentsLast.First() == '[' && segmentsLast.Last() == ']' && segmentsLast[1] == '"' && segmentsLast[^2] == '"')
|
||||
jsonLines.Add($"{segmentsLast},");
|
||||
else if (segmentsLast.First() == '"' && segmentsLast.Last() == '"')
|
||||
jsonLines.Add($"{segmentsLast},");
|
||||
else if (segmentsLast.First() == '"' && segmentsLast.Last() == '"')
|
||||
@ -300,41 +373,10 @@ internal static partial class HelperMarkdown
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!jsonLines.Any())
|
||||
(result, keyValuePairs) = (null, null);
|
||||
if (jsonLines.Any())
|
||||
(result, keyValuePairs, results) = Get(jsonLines);
|
||||
else
|
||||
{
|
||||
string afterTrim;
|
||||
string jsonLinesLast = jsonLines.Last();
|
||||
jsonLines.RemoveAt(jsonLines.Count - 1);
|
||||
jsonLines.Add(jsonLinesLast[..^1]);
|
||||
jsonLines.Insert(0, "{");
|
||||
jsonLines.Add("}");
|
||||
result = string.Join(Environment.NewLine, jsonLines);
|
||||
keyValuePairs = JsonSerializer.Deserialize(result, DictionaryStringAndJsonElementSourceGenerationContext.Default.DictionaryStringJsonElement);
|
||||
if (keyValuePairs is null)
|
||||
throw new NullReferenceException(nameof(keyValuePairs));
|
||||
result = JsonSerializer.Serialize(keyValuePairs, DictionaryStringAndJsonElementSourceGenerationContext.Default.DictionaryStringJsonElement);
|
||||
jsonLines = result.Split(Environment.NewLine).ToList();
|
||||
foreach (string jsonLine in jsonLines)
|
||||
{
|
||||
afterTrim = jsonLine.Trim();
|
||||
if (string.IsNullOrEmpty(afterTrim) || afterTrim.First() is '{' or '}')
|
||||
continue;
|
||||
segments = afterTrim.Split(": ");
|
||||
if (segments.Length != 2)
|
||||
{
|
||||
result = null;
|
||||
results.Clear();
|
||||
keyValuePairs = null;
|
||||
break;
|
||||
}
|
||||
if (afterTrim[^1] != ',')
|
||||
results.Add(afterTrim[1..].Replace("\": ", ": "));
|
||||
else
|
||||
results.Add(afterTrim[1..^1].Replace("\": ", ": "));
|
||||
}
|
||||
}
|
||||
(result, keyValuePairs, results) = (null, null, new());
|
||||
return (result, keyValuePairs, results.ToArray());
|
||||
}
|
||||
|
||||
@ -691,7 +733,7 @@ internal static partial class HelperMarkdown
|
||||
return result;
|
||||
}
|
||||
|
||||
private static (string?, string?) GetRelativePath(List<(MarkdownFile MarkdownFile, string[] Lines)> collection, MarkdownFile markdownFile, string file)
|
||||
private static (string?, string?) GetRelativePath(string[] allSourceFiles, List<(MarkdownFile MarkdownFile, string[] Lines)> collection, MarkdownFile markdownFile, string file)
|
||||
{
|
||||
string? title;
|
||||
string? relativePath;
|
||||
@ -707,22 +749,23 @@ internal static partial class HelperMarkdown
|
||||
else
|
||||
{
|
||||
results.Clear();
|
||||
string fileLowered = file.ToLower();
|
||||
foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection)
|
||||
foreach (string allSourceFile in allSourceFiles)
|
||||
{
|
||||
if (item.MarkdownFile.File.ToLower() != fileLowered)
|
||||
if (allSourceFile.EndsWith(".md"))
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1));
|
||||
if (allSourceFile != file)
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(allSourceFile)), Path.GetFileNameWithoutExtension(allSourceFile)));
|
||||
}
|
||||
if (results.Count == 1)
|
||||
(relativePath, title) = (results.First().RelativePath.Replace(" ", "%20"), results.First().Title);
|
||||
else
|
||||
{
|
||||
results.Clear();
|
||||
string fileFullPath = Path.GetFullPath(fileLowered);
|
||||
string fileLowered = file.ToLower();
|
||||
foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection)
|
||||
{
|
||||
if (Path.GetFullPath(item.MarkdownFile.File).ToLower() != fileFullPath)
|
||||
if (item.MarkdownFile.File.ToLower() != fileLowered)
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1));
|
||||
}
|
||||
@ -731,17 +774,72 @@ internal static partial class HelperMarkdown
|
||||
else
|
||||
{
|
||||
results.Clear();
|
||||
string spaceNaming = fileFullPath.Replace(" ", "%20");
|
||||
foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection)
|
||||
foreach (string allSourceFile in allSourceFiles)
|
||||
{
|
||||
if (Path.GetFullPath(item.MarkdownFile.File).ToLower().Replace(" ", "%20") != spaceNaming)
|
||||
if (allSourceFile.EndsWith(".md"))
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1));
|
||||
if (allSourceFile.ToLower() != fileLowered)
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(allSourceFile)), Path.GetFileNameWithoutExtension(allSourceFile)));
|
||||
}
|
||||
if (results.Count == 1)
|
||||
(relativePath, title) = (results.First().RelativePath.Replace(" ", "%20"), results.First().Title);
|
||||
else
|
||||
(relativePath, title) = (null, null);
|
||||
{
|
||||
results.Clear();
|
||||
string fileFullPath = Path.GetFullPath(fileLowered);
|
||||
foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection)
|
||||
{
|
||||
if (Path.GetFullPath(item.MarkdownFile.File).ToLower() != fileFullPath)
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1));
|
||||
}
|
||||
if (results.Count == 1)
|
||||
(relativePath, title) = (results.First().RelativePath.Replace(" ", "%20"), results.First().Title);
|
||||
else
|
||||
{
|
||||
results.Clear();
|
||||
foreach (string allSourceFile in allSourceFiles)
|
||||
{
|
||||
if (allSourceFile.EndsWith(".md"))
|
||||
continue;
|
||||
if (allSourceFile.ToLower() != fileFullPath)
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(allSourceFile)), Path.GetFileNameWithoutExtension(allSourceFile)));
|
||||
}
|
||||
if (results.Count == 1)
|
||||
(relativePath, title) = (results.First().RelativePath.Replace(" ", "%20"), results.First().Title);
|
||||
else
|
||||
{
|
||||
results.Clear();
|
||||
string spaceNaming = fileFullPath.Replace(" ", "%20");
|
||||
foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection)
|
||||
{
|
||||
if (Path.GetFullPath(item.MarkdownFile.File).ToLower().Replace(" ", "%20") != spaceNaming)
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1));
|
||||
}
|
||||
if (results.Count == 1)
|
||||
(relativePath, title) = (results.First().RelativePath.Replace(" ", "%20"), results.First().Title);
|
||||
else
|
||||
{
|
||||
results.Clear();
|
||||
foreach (string allSourceFile in allSourceFiles)
|
||||
{
|
||||
if (allSourceFile.EndsWith(".md"))
|
||||
continue;
|
||||
if (Path.GetFullPath(allSourceFile).ToLower().Replace(" ", "%20") != spaceNaming)
|
||||
continue;
|
||||
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(allSourceFile)), Path.GetFileNameWithoutExtension(allSourceFile)));
|
||||
}
|
||||
if (results.Count == 1)
|
||||
(relativePath, title) = (results.First().RelativePath.Replace(" ", "%20"), results.First().Title);
|
||||
else
|
||||
(relativePath, title) = (null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -767,6 +865,7 @@ internal static partial class HelperMarkdown
|
||||
string segmentsALast;
|
||||
string? relativePath;
|
||||
string segmentsBFirst;
|
||||
string[] allSourceFiles = Directory.GetFiles(record.Source, "*", SearchOption.AllDirectories);
|
||||
List<(MarkdownFile MarkdownFile, string[] Lines)> sourceCollection = record.StartAt is null ? collection : GetCollection(appSettings, record);
|
||||
foreach ((MarkdownFile markdownFile, string[] lines) in collection)
|
||||
{
|
||||
@ -795,7 +894,7 @@ internal static partial class HelperMarkdown
|
||||
checkFileName = fileName.ToLower().Replace("%20", "-").Replace(' ', '-');
|
||||
checkName = Path.Combine(directory, checkFileName);
|
||||
segmentsC = segmentsA.First().Split('[');
|
||||
(relativePath, title) = GetRelativePath(sourceCollection, markdownFile, file);
|
||||
(relativePath, title) = GetRelativePath(allSourceFiles, sourceCollection, markdownFile, file);
|
||||
if (relativePath is null)
|
||||
{
|
||||
logger.LogInformation("Didn't find {line} in <{file}>", lines[i], markdownFile.FileNameWithoutExtension);
|
||||
|
Loading…
x
Reference in New Issue
Block a user