207 lines
8.2 KiB
C#
207 lines
8.2 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
|
|
namespace File_Folder_Helper.Helpers;
|
|
|
|
internal static class HelperCreateNoteFiles
|
|
{
|
|
|
|
private static void CleanExistingFiles(string directory, long ticks)
|
|
{
|
|
string check;
|
|
string[] lines;
|
|
string checkFile;
|
|
string? fileDirectory;
|
|
string checkDirectory;
|
|
bool circularReference;
|
|
string fileNameWithoutExtension;
|
|
char altDrive = directory[0] is 'c' or 'C' ? 'D' : 'C';
|
|
string[] files = Directory.GetFiles(directory, "*.md", SearchOption.AllDirectories);
|
|
string match = ",*** *** *** *** *** *** *** *** ***,*** *** *** *** *** *** *** *** ***,*** *** *** *** *** *** *** *** ***,,TODO:,*** *** *** *** *** *** *** *** ***,*** *** *** *** *** *** *** *** ***,,Notes:,*** *** *** *** *** *** *** *** ***,*** *** *** *** *** *** *** *** ***";
|
|
foreach (string file in files)
|
|
{
|
|
lines = File.ReadAllLines(file);
|
|
if (lines.Length < 1)
|
|
continue;
|
|
circularReference = false;
|
|
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
if (!lines[i].Contains($"[[{fileNameWithoutExtension}]]"))
|
|
continue;
|
|
lines[i] = lines[i].Replace("[[", "__").Replace("]]", "__");
|
|
if (!circularReference)
|
|
circularReference = true;
|
|
}
|
|
if (circularReference)
|
|
File.WriteAllLines(file, lines);
|
|
lines[0] = string.Empty;
|
|
check = string.Join(',', lines);
|
|
if (check != match)
|
|
continue;
|
|
fileDirectory = Path.GetDirectoryName(file);
|
|
if (string.IsNullOrEmpty(fileDirectory))
|
|
continue;
|
|
checkDirectory = $"{altDrive}:/{ticks}/{fileDirectory[3..]}";
|
|
if (!Directory.Exists(checkDirectory))
|
|
_ = Directory.CreateDirectory(checkDirectory);
|
|
checkFile = $"{altDrive}:/{ticks}/{file[3..]}";
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
File.Move(file, checkFile);
|
|
}
|
|
_ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(directory);
|
|
}
|
|
|
|
private static void CreateDailyNotes(string argsZero, long ticks)
|
|
{
|
|
string file;
|
|
string directory;
|
|
string weekOfYear;
|
|
DateTime dateTime;
|
|
string lastDirectory = string.Empty;
|
|
DateTime startDateTime = DateTime.Now.AddDays(1);
|
|
Calendar calendar = new CultureInfo("en-US").Calendar;
|
|
double totalDays = new TimeSpan(DateTime.Now.AddDays(1000).Ticks - startDateTime.Ticks).TotalDays;
|
|
int days = (int)Math.Ceiling(totalDays);
|
|
for (int i = 0; i < days; i++)
|
|
{
|
|
dateTime = startDateTime.AddDays(i);
|
|
weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
directory = Path.Combine(argsZero, ticks.ToString(), dateTime.ToString("yyyy"), $"Week_{weekOfYear}");
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
file = string.Concat(Path.Combine(directory, $"{dateTime:yyyy-MM-dd}.md"));
|
|
if (File.Exists(file))
|
|
continue;
|
|
File.WriteAllLines(file, new string[]
|
|
{
|
|
"---",
|
|
"type: daily-note",
|
|
$"created: {dateTime:yyyy-MM-dd}",
|
|
"---",
|
|
string.Empty,
|
|
$"# {dateTime:yyyy-MM-dd dddd}",
|
|
string.Empty,
|
|
"```bash",
|
|
string.Empty,
|
|
"```",
|
|
});
|
|
if (directory != lastDirectory)
|
|
{
|
|
Directory.SetCreationTime(directory, dateTime);
|
|
Directory.SetLastWriteTime(directory, dateTime);
|
|
}
|
|
lastDirectory = directory;
|
|
}
|
|
}
|
|
|
|
private static void CreateImportFiles(long ticks, List<string> importFiles)
|
|
{
|
|
bool csv;
|
|
bool tsv;
|
|
string file;
|
|
string type;
|
|
string title;
|
|
string[] lines;
|
|
string[] links;
|
|
int bodyKey = 4;
|
|
int typeKey = 0;
|
|
string linkText;
|
|
int linksKey = 3;
|
|
int titleKey = 1;
|
|
string[] columns;
|
|
string? directory;
|
|
string[] segments;
|
|
int descriptionKey = 2;
|
|
string destinationDirectory;
|
|
DateTime dateTime = new(ticks);
|
|
StringBuilder stringBuilder = new();
|
|
string csvHeader = "type,title,description,links,body";
|
|
string tsvHeader = "type\ttitle\tdescription\tlinks\tbody";
|
|
int expectedCount = csvHeader.Length - csvHeader.Replace(",", string.Empty).Length + 1;
|
|
foreach (string importFile in importFiles)
|
|
{
|
|
csv = false;
|
|
tsv = false;
|
|
directory = Path.GetDirectoryName(importFile);
|
|
if (directory is null)
|
|
continue;
|
|
lines = File.ReadAllLines(importFile);
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
if (lines[i] == csvHeader)
|
|
(csv, tsv) = (true, false);
|
|
else if (lines[i] == tsvHeader)
|
|
(csv, tsv) = (false, true);
|
|
else
|
|
break;
|
|
continue;
|
|
}
|
|
if (csv)
|
|
columns = lines[i].Split(',');
|
|
else if (tsv)
|
|
columns = lines[i].Split('\t');
|
|
else
|
|
continue;
|
|
if (columns.Length != expectedCount)
|
|
continue;
|
|
_ = stringBuilder.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);
|
|
foreach (string link in links)
|
|
{
|
|
segments = link.Split(':');
|
|
if (segments.Length == 1)
|
|
_ = stringBuilder.AppendLine($"- [[{segments.First()}]]");
|
|
else if (segments.Length == 2)
|
|
_ = stringBuilder.AppendLine($"- [{segments.First()}]({segments.Last()})");
|
|
else
|
|
continue;
|
|
}
|
|
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}",
|
|
"---",
|
|
string.Empty,
|
|
$"# {title}",
|
|
string.Empty,
|
|
stringBuilder.ToString(),
|
|
string.Empty,
|
|
columns[bodyKey].Trim(),
|
|
string.Empty,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static void CreateNoteFiles(string argsZero)
|
|
{
|
|
long ticks = DateTime.Now.Ticks;
|
|
CleanExistingFiles(argsZero, ticks);
|
|
List<string> importFiles = new();
|
|
importFiles.AddRange(Directory.GetFiles(argsZero, "*.csv", SearchOption.TopDirectoryOnly));
|
|
importFiles.AddRange(Directory.GetFiles(argsZero, "*.tsv", SearchOption.TopDirectoryOnly));
|
|
if (!importFiles.Any())
|
|
CreateDailyNotes(argsZero, ticks);
|
|
else
|
|
CreateImportFiles(ticks, importFiles);
|
|
}
|
|
|
|
} |