ExcludeSchemes

This commit is contained in:
Mike Phares 2023-07-19 08:59:36 -07:00
parent 40b12d17f9
commit 2b6e65b730
3 changed files with 68 additions and 12 deletions

View File

@ -46,7 +46,7 @@ internal static partial class HelperMarkdown
internal static string[] GetFiles(AppSettings appSettings, string directory)
{
string[] results = Directory.GetFiles(directory, "*.md", SearchOption.AllDirectories).
Where(l => !appSettings.Exclude.Any(m => l.Contains(m))).ToArray();
Where(l => !appSettings.ExcludeDirectoryNames.Any(m => l.Contains(m))).ToArray();
return results;
}
@ -151,6 +151,14 @@ internal static partial class HelperMarkdown
return results;
}
private static List<(MarkdownFile, string[])> GetCollection(AppSettings appSettings, Record record)
{
List<(MarkdownFile, string[])> results;
string[] files = GetFiles(appSettings, record.Source);
results = GetCollection(appSettings, files);
return results;
}
private static int SetFrontMatterAndH1(AppSettings appSettings, List<(MarkdownFile, string[])> collection)
{
int result = 0;
@ -342,8 +350,7 @@ internal static partial class HelperMarkdown
private static Dictionary<string, List<MarkdownFile>> GetKeyValuePairs(AppSettings appSettings, Record record)
{
Dictionary<string, List<MarkdownFile>> results;
string[] files = GetFiles(appSettings, record.Source);
List<(MarkdownFile MarkdownFile, string[] Lines)> collection = GetCollection(appSettings, files);
List<(MarkdownFile MarkdownFile, string[] Lines)> collection = GetCollection(appSettings, record);
results = GetKeyValuePairs(collection);
return results;
}
@ -507,6 +514,50 @@ internal static partial class HelperMarkdown
return result;
}
private static (string?, string?) GetRelativePath(List<(MarkdownFile MarkdownFile, string[] Lines)> collection, MarkdownFile markdownFile, string file)
{
string? title;
string? relativePath;
List<(string RelativePath, string Title)> results = new();
foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection)
{
if (item.MarkdownFile.File != file)
continue;
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1));
}
if (results.Count == 1)
(relativePath, title) = (results.First().RelativePath, results.First().Title);
else
{
results.Clear();
string fileLowered = file.ToLower();
foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection)
{
if (item.MarkdownFile.File.ToLower() != fileLowered)
continue;
results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1));
}
if (results.Count == 1)
(relativePath, title) = (results.First().RelativePath, results.First().Title);
else
{
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, results.First().Title);
else
(relativePath, title) = (null, null);
}
}
return (relativePath, title);
}
private static int ConvertFileToSlugName(AppSettings appSettings, ILogger<Worker> logger, Record record, List<(MarkdownFile MarkdownFile, string[] Lines)> collection)
{
int result = 0;
@ -526,7 +577,7 @@ internal static partial class HelperMarkdown
string segmentsALast;
string? relativePath;
string segmentsBFirst;
Dictionary<string, List<MarkdownFile>> keyValuePairs = record.StartAt is null ? GetKeyValuePairs(collection) : GetKeyValuePairs(appSettings, record);
List<(MarkdownFile MarkdownFile, string[] Lines)> sourceCollection = record.StartAt is null ? collection : GetCollection(appSettings, record);
foreach ((MarkdownFile markdownFile, string[] lines) in collection)
{
if (markdownFile.FileNameWithoutExtension == "index")
@ -540,7 +591,7 @@ internal static partial class HelperMarkdown
if (segmentsA.Length != 2)
continue;
segmentsALast = segmentsA.Last();
if (segmentsALast.StartsWith("http:") || segmentsALast.StartsWith("https:") || segmentsALast.StartsWith("rdp:") || segmentsALast.StartsWith("onenote:"))
if (appSettings.ExcludeSchemes.Any(l => segmentsALast.StartsWith(l)))
continue;
segmentsB = segmentsALast.Split(")");
if (segmentsB.Length != 2)
@ -554,7 +605,7 @@ internal static partial class HelperMarkdown
checkFileName = fileName.ToLower().Replace("%20", "-").Replace(' ', '-');
checkName = Path.Combine(directory, checkFileName);
segmentsC = segmentsA.First().Split('[');
(relativePath, title) = GetRelativePath(keyValuePairs, markdownFile, file);
(relativePath, title) = GetRelativePath(sourceCollection, markdownFile, file);
if (relativePath is null)
{
logger.LogInformation("Didn't find {line} in <{file}>", lines[i], markdownFile.FileNameWithoutExtension);
@ -748,7 +799,7 @@ internal static partial class HelperMarkdown
if (segmentsA.Length != 2)
continue;
segmentsALast = segmentsA.Last();
if (segmentsALast.StartsWith("http:") || segmentsALast.StartsWith("https:") || segmentsALast.StartsWith("rdp:") || segmentsALast.StartsWith("onenote:"))
if (appSettings.ExcludeSchemes.Any(l => segmentsALast.StartsWith(l)))
continue;
segmentsB = segmentsALast.Split(")");
if (segmentsB.Length != 2)

View File

@ -5,7 +5,8 @@ namespace File_Folder_Helper.Models;
public record AppSettings(string Company,
string DefaultNoteType,
string[] Exclude,
string[] ExcludeDirectoryNames,
string[] ExcludeSchemes,
string WorkingDirectoryName)
{

View File

@ -9,7 +9,8 @@ public class AppSettings
public string? Company { get; set; }
public string? DefaultNoteType { get; set; }
public string[]? Exclude { get; set; }
public string[]? ExcludeDirectoryNames { get; set; }
public string[]? ExcludeSchemes { get; set; }
public string? WorkingDirectoryName { get; set; }
public override string ToString()
@ -25,14 +26,17 @@ public class AppSettings
throw new NullReferenceException(nameof(appSettings.Company));
if (appSettings?.DefaultNoteType is null)
throw new NullReferenceException(nameof(appSettings.DefaultNoteType));
if (appSettings?.Exclude is null)
throw new NullReferenceException(nameof(appSettings.Exclude));
if (appSettings?.ExcludeDirectoryNames is null)
throw new NullReferenceException(nameof(appSettings.ExcludeDirectoryNames));
if (appSettings?.ExcludeSchemes is null)
throw new NullReferenceException(nameof(appSettings.ExcludeSchemes));
if (appSettings?.WorkingDirectoryName is null)
throw new NullReferenceException(nameof(appSettings.WorkingDirectoryName));
result = new(
appSettings.Company,
appSettings.DefaultNoteType,
appSettings.Exclude,
appSettings.ExcludeDirectoryNames,
appSettings.ExcludeSchemes,
appSettings.WorkingDirectoryName
);
return result;