Esempio n. 1
0
        public JournalReader(IFileSystem fileSystem, string filePath)
        {
            FilePath = filePath;
            var lines = fileSystem.File.ReadAllLines(FilePath).ToList();

            var headers = lines.Where(HeaderValidator.IsValid).ToList();
            var h1      = headers.FirstOrDefault()?.Replace("# ", "");

            if (h1 != null && headers.Count > 1 && DateTime.TryParse(h1, out _))
            {
                Headers = lines.Where(x => x.StartsWith("#")).Skip(1).ToList();
            }
            else
            {
                Headers = lines.Where(x => x.StartsWith("#")).ToList();
            }

            var bodyStartIndex = lines.FindLastIndex(s => s == JournalFrontMatter.BlockIndicator) + 1;

            RawBody     = string.Join(Environment.NewLine, lines.Skip(bodyStartIndex));
            FrontMatter = JournalFrontMatter.FromFilePath(fileSystem, filePath);
            EntryName   = fileSystem.Path.GetFileNameWithoutExtension(FilePath) ?? throw new InvalidOperationException();

            if (!Journal.IsCompiledEntry(EntryName))
            {
                EntryDate = Journal.FileNamePattern.Parse(EntryName).Value;
            }
        }