Relative Path to relative to content
This commit is contained in:
@ -1,12 +1,15 @@
|
||||
using Humanizer;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperCreateNoteFiles
|
||||
internal static partial class HelperCreateNoteFiles
|
||||
{
|
||||
|
||||
[GeneratedRegex("[^a-z0-9-]")]
|
||||
private static partial Regex AlphaNumOnly();
|
||||
|
||||
private static void CleanExistingFiles(string directory, long ticks)
|
||||
{
|
||||
string check;
|
||||
@ -97,31 +100,102 @@ internal static class HelperCreateNoteFiles
|
||||
}
|
||||
}
|
||||
|
||||
private static string? GetTags(string tagsText)
|
||||
{
|
||||
string? result;
|
||||
StringBuilder stringBuilder = new();
|
||||
if (string.IsNullOrEmpty(tagsText))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
string[] segments;
|
||||
_ = stringBuilder.AppendLine("tags:");
|
||||
string[] tags = tagsText.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string tag in tags)
|
||||
{
|
||||
segments = tag.Split(':');
|
||||
_ = stringBuilder.AppendLine($"- '{segments.First()}'");
|
||||
}
|
||||
result = stringBuilder.ToString().Trim();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string? GetLinks(string type, string linksText)
|
||||
{
|
||||
string? result;
|
||||
StringBuilder stringBuilder = new();
|
||||
if (!string.IsNullOrEmpty(linksText))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
string linkLower;
|
||||
string[] segments;
|
||||
string[] links = linksText.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string link in links)
|
||||
{
|
||||
segments = link.Split(':');
|
||||
linkLower = AlphaNumOnly().Replace(segments.First().Trim().ToLower(), "-").Replace("--", "-");
|
||||
if (segments.Length == 1)
|
||||
_ = stringBuilder.AppendLine($"- [[{type}/{linkLower}]]");
|
||||
else if (segments.Length == 2)
|
||||
_ = stringBuilder.AppendLine($"- [{type}/{linkLower}]({segments.Last()})");
|
||||
else
|
||||
continue;
|
||||
}
|
||||
result = stringBuilder.ToString().Trim();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string? GetAttributes(string[] columns, string[]? headerColumns, int expectedCount)
|
||||
{
|
||||
string? result;
|
||||
if (headerColumns is null || columns.Length <= expectedCount)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
StringBuilder stringBuilder = new();
|
||||
for (int j = expectedCount; j < columns.Length; j++)
|
||||
{
|
||||
if (headerColumns.Length <= j)
|
||||
continue;
|
||||
_ = stringBuilder.AppendLine($"{headerColumns[j].Trim()}: '{columns[j].Trim()}'");
|
||||
}
|
||||
result = stringBuilder.ToString().Trim();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void CreateImportFiles(long ticks, List<string> importFiles)
|
||||
{
|
||||
bool csv;
|
||||
bool tsv;
|
||||
string file;
|
||||
string text;
|
||||
string type;
|
||||
string title;
|
||||
string? tags;
|
||||
string? links;
|
||||
string[] lines;
|
||||
string[] links;
|
||||
int bodyKey = 4;
|
||||
int bodyKey = 5;
|
||||
int tagsKey = 3;
|
||||
int typeKey = 0;
|
||||
string linkText;
|
||||
int linksKey = 3;
|
||||
string fileName;
|
||||
string tagsText;
|
||||
int linksKey = 4;
|
||||
int titleKey = 1;
|
||||
string linksText;
|
||||
string[] columns;
|
||||
string? directory;
|
||||
string[] segments;
|
||||
string? attributes;
|
||||
int descriptionKey = 2;
|
||||
string[]? headerColumns;
|
||||
string destinationDirectory;
|
||||
List<string> allLines = new();
|
||||
DateTime dateTime = new(ticks);
|
||||
StringBuilder attributes = new();
|
||||
StringBuilder keyValuePairLinks = new();
|
||||
string csvHeader = "type,title,description,links,body";
|
||||
string tsvHeader = "type\ttitle\tdescription\tlinks\tbody";
|
||||
string csvHeader = "type,title,description,tags,links,body";
|
||||
string tsvHeader = "type\ttitle\tdescription\ttags\tlinks\tbody";
|
||||
int expectedCount = csvHeader.Length - csvHeader.Replace(",", string.Empty).Length + 1;
|
||||
foreach (string importFile in importFiles)
|
||||
{
|
||||
@ -158,58 +232,42 @@ internal static class HelperCreateNoteFiles
|
||||
continue;
|
||||
if (columns.Length < expectedCount)
|
||||
continue;
|
||||
_ = attributes.Clear();
|
||||
_ = keyValuePairLinks.Clear();
|
||||
title = columns[titleKey].Trim();
|
||||
linkText = columns[linksKey].Trim();
|
||||
type = columns[typeKey].Trim().ToLower().Replace(' ', '-');
|
||||
if (string.IsNullOrEmpty(linkText))
|
||||
links = Array.Empty<string>();
|
||||
else
|
||||
links = linkText.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (headerColumns is not null && columns.Length > expectedCount)
|
||||
{
|
||||
for (int j = expectedCount; j < columns.Length; j++)
|
||||
{
|
||||
if (headerColumns.Length <= j)
|
||||
continue;
|
||||
_ = attributes.AppendLine($"{headerColumns[j].Trim().Camelize()}: '{columns[j].Trim()}'");
|
||||
}
|
||||
}
|
||||
foreach (string link in links)
|
||||
{
|
||||
segments = link.Split(':');
|
||||
if (segments.Length == 1)
|
||||
_ = keyValuePairLinks.AppendLine($"- [[{segments.First()}]]");
|
||||
else if (segments.Length == 2)
|
||||
_ = keyValuePairLinks.AppendLine($"- [{segments.First()}]({segments.Last()})");
|
||||
else
|
||||
continue;
|
||||
}
|
||||
tagsText = columns[tagsKey].Trim();
|
||||
linksText = columns[linksKey].Trim();
|
||||
fileName = AlphaNumOnly().Replace(title.ToLower(), "-").Replace("--", "-");
|
||||
type = AlphaNumOnly().Replace(columns[typeKey].Trim().ToLower(), "-").Replace("--", "-");
|
||||
tags = GetTags(tagsText);
|
||||
links = GetLinks(type, linksText);
|
||||
attributes = GetAttributes(columns, headerColumns, expectedCount);
|
||||
destinationDirectory = Path.Combine(directory, type);
|
||||
if (!Directory.Exists(destinationDirectory))
|
||||
_ = Directory.CreateDirectory(destinationDirectory);
|
||||
file = Path.Combine(destinationDirectory, $"{title.ToLower().Replace(' ', '-')}.md");
|
||||
File.WriteAllLines(file, new string[]
|
||||
{
|
||||
"---",
|
||||
$"type: '{type}'",
|
||||
$"title: '{title}'",
|
||||
$"description: '{columns[descriptionKey].Trim()}'",
|
||||
$"created: {dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}",
|
||||
$"updated: {dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}",
|
||||
attributes.ToString(),
|
||||
"---",
|
||||
string.Empty,
|
||||
$"# {title}",
|
||||
string.Empty,
|
||||
keyValuePairLinks.ToString(),
|
||||
string.Empty,
|
||||
$"## Comment {dateTime:yyyy-MM-dd}",
|
||||
string.Empty,
|
||||
columns[bodyKey].Trim(),
|
||||
string.Empty,
|
||||
});
|
||||
file = Path.Combine(destinationDirectory, $"{fileName}.md");
|
||||
allLines.Clear();
|
||||
allLines.Add("---");
|
||||
allLines.Add($"type: '{type}'");
|
||||
allLines.Add($"title: '{title}'");
|
||||
allLines.Add($"description: '{columns[descriptionKey].Trim()}'");
|
||||
allLines.Add($"created: {dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}");
|
||||
allLines.Add($"updated: {dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}");
|
||||
if (!string.IsNullOrEmpty(tags))
|
||||
allLines.Add(tags);
|
||||
if (!string.IsNullOrEmpty(attributes))
|
||||
allLines.Add(attributes);
|
||||
allLines.Add("---");
|
||||
allLines.Add(string.Empty);
|
||||
allLines.Add($"# {title}");
|
||||
allLines.Add(string.Empty);
|
||||
if (!string.IsNullOrEmpty(links))
|
||||
allLines.Add(links);
|
||||
allLines.Add(string.Empty);
|
||||
allLines.Add($"## Comment {dateTime:yyyy-MM-dd}");
|
||||
allLines.Add(string.Empty);
|
||||
allLines.Add(columns[bodyKey].Trim());
|
||||
allLines.Add(string.Empty);
|
||||
text = string.Join(Environment.NewLine, allLines);
|
||||
File.WriteAllText(file, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user