Ejemplo n.º 1
0
        /// <summary>
        /// Creates a single journal entry comprised of all entries found which match the specified criteria.
        /// </summary>
        /// <param name="range">The date range to search for entries. Dates are inclusive. Null values assume all entries are desired.</param>
        /// <param name="tags">Filters entries by tag. Null values assumes all tags are desired.</param>
        /// <param name="allTagsRequired">Requires that each matching entry contain all tags specified by the `tags` parameter.</param>
        /// <param name="overwrite">True to overwrite an existing compiled entry with the same name. Otherwise, an exception is thrown.</param>
        public void CreateCompiledEntry(DateRange range, string[] tags, bool allTagsRequired, bool overwrite)
        {
            List <JournalEntryFile> entries;
            var hasTags = tags != null && tags.Length > 0;

            if (hasTags)
            {
                if (allTagsRequired)
                {
                    entries = CreateIndex <JournalEntryFile>(range, tags)
                              .SelectMany(x => x.Entries)
                              .OrderBy(x => x)
                              .Distinct()
                              .ToList();
                }
                else
                {
                    entries = CreateIndex <JournalEntryFile>(range)
                              .Where(x => tags.Contains(x.Tag))
                              .SelectMany(x => x.Entries)
                              .OrderBy(x => x)
                              .Distinct()
                              .ToList();
                }
            }
            else
            {
                entries = CreateIndex <JournalEntryFile>(range)
                          .SelectMany(x => x.Entries)
                          .OrderBy(x => x)
                          .Distinct()
                          .ToList();
            }

            if (entries.Count == 0)
            {
                throw new InvalidOperationException("No journal entries found matching the specified criteria. Please change your criteria and try again.");
            }

            if (range == null)
            {
                range = new DateRange(entries.First().EntryDate, entries.Last().EntryDate);
            }

            var journalWriter = _readerWriterFactory.CreateWriter();
            var entryFilePath = journalWriter.GetCompiledJournalEntryFilePath(range);

            if (journalWriter.EntryExists(entryFilePath) && !overwrite)
            {
                throw new JournalEntryAlreadyExistsException(entryFilePath);
            }

            var aggregatedTags = entries.SelectMany(x => x.Tags).Distinct();
            var content        = string.Join(Environment.NewLine, entries.Select(x => x.Body));

            var frontMatter = new JournalFrontMatter(aggregatedTags);

            journalWriter.CreateCompiled(frontMatter, entryFilePath, content);
            _systemProcess.Start(entryFilePath);
        }
Ejemplo n.º 2
0
        public void CreateCompiledEntry(ICollection <IJournalEntry> entries, bool overwrite)
        {
            if (entries == null || !entries.Any())
            {
                throw new ArgumentException("No entries were provided. At least one entry must be provided.", nameof(entries));
            }

            var convertedEntries = entries.Select(x => new JournalEntryFile(x.GetReader())).OrderBy(x => x.EntryDate).ToList();
            var range            = new DateRange(convertedEntries.First().EntryDate, convertedEntries.Last().EntryDate);

            var journalWriter = _readerWriterFactory.CreateWriter();
            var entryFilePath = journalWriter.GetCompiledJournalEntryFilePath(range);

            if (journalWriter.EntryExists(entryFilePath) && !overwrite)
            {
                throw new JournalEntryAlreadyExistsException(entryFilePath);
            }

            var aggregatedTags = convertedEntries.SelectMany(x => x.Tags).Distinct();
            var content        = string.Join(Environment.NewLine, convertedEntries.Select(x => x.Body));

            var frontMatter = new JournalFrontMatter(aggregatedTags);

            journalWriter.CreateCompiled(frontMatter, entryFilePath, content);
            _systemProcess.Start(entryFilePath);
        }
Ejemplo n.º 3
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;
            }
        }
Ejemplo n.º 4
0
        public void AppendEntryContent(LocalDate entryDate, string[] body, string heading, string[] tags, string readme, out IEnumerable <string> warnings)
        {
            var journalWriter = _readerWriterFactory.CreateWriter();
            var entryFilePath = journalWriter.GetJournalEntryFilePath(entryDate);

            IJournalFrontMatter frontMatter;
            JournalEntryBody    journalBody;
            var readmeParser = new ReadmeParser(readme);

            if (journalWriter.EntryExists(entryFilePath))
            {
                var journalReader = _readerWriterFactory.CreateReader(entryFilePath);

                frontMatter = journalReader.FrontMatter;
                frontMatter.AppendTags(tags);
                journalBody = new JournalEntryBody(journalReader.RawBody);

                warnings = readmeParser.IsValid
                    ? new List <string>
                {
                    "This journal entry already has a Readme date applied. Readme dates cannot be overwritten using this method. " +
                    "If you want to edit the Readme date, open the entry and either change it manually, or delete it entirely and run this " +
                    "method again."
                }
                    : new List <string>();
            }
            else
            {
                warnings = new List <string>();
                var readmeExpression = readmeParser.ToExpression(entryDate);
                frontMatter = new JournalFrontMatter(tags, readmeExpression);
                journalBody = new JournalEntryBody();
            }

            if (body != null && body.Any())
            {
                if (string.IsNullOrEmpty(heading))
                {
                    journalBody.AddOrAppendToDefaultHeader(entryDate, body);
                }
                else
                {
                    journalBody.AddOrAppendToCustomHeader(heading, body);
                }
            }

            journalWriter.Create(entryFilePath, frontMatter, journalBody.ToString());
        }
Ejemplo n.º 5
0
        public void CreateNewEntry(LocalDate entryDate, string[] tags, string readme)
        {
            var journalWriter = _readerWriterFactory.CreateWriter();
            var entryFilePath = journalWriter.GetJournalEntryFilePath(entryDate);

            if (journalWriter.EntryExists(entryFilePath))
            {
                throw new JournalEntryAlreadyExistsException(entryFilePath);
            }

            var parser      = string.IsNullOrWhiteSpace(readme) ? null : new ReadmeParser(readme, entryDate);
            var frontMatter = new JournalFrontMatter(tags, parser);

            journalWriter.Create(frontMatter, entryFilePath, entryDate);
            _systemProcess.Start(entryFilePath);
        }
Ejemplo n.º 6
0
        public void CreateNewEntry(LocalDate entryDate, string[] tags, string readme)
        {
            var journalWriter = _readerWriterFactory.CreateWriter();
            var entryFilePath = journalWriter.GetJournalEntryFilePath(entryDate);

            if (journalWriter.EntryExists(entryFilePath))
            {
                throw new JournalEntryAlreadyExistsException(entryFilePath);
            }

            var readmeExpression = new ReadmeParser(readme).ToExpression(entryDate);
            var frontMatter      = new JournalFrontMatter(tags, readmeExpression);
            var header           = $"# {entryDate}";

            journalWriter.Create(entryFilePath, frontMatter, header);
            _systemProcess.Start(entryFilePath);
        }
Ejemplo n.º 7
0
        public void AppendEntryContent(LocalDate entryDate, string[] body, string heading, string[] tags)
        {
            var journalWriter = _readerWriterFactory.CreateWriter();
            var entryFilePath = journalWriter.GetJournalEntryFilePath(entryDate);

            IJournalFrontMatter frontMatter;
            JournalEntryBody    journalBody;

            if (journalWriter.EntryExists(entryFilePath))
            {
                var journalReader = _readerWriterFactory.CreateReader(entryFilePath);

                frontMatter = journalReader.FrontMatter;
                frontMatter.AppendTags(tags);
                journalBody = new JournalEntryBody(journalReader.RawBody);
            }
            else
            {
                frontMatter = new JournalFrontMatter(tags);
                journalBody = new JournalEntryBody();
            }

            if (body != null && body.Any())
            {
                if (string.IsNullOrEmpty(heading))
                {
                    journalBody.AddOrAppendToDefaultHeader(entryDate, body);
                }
                else
                {
                    journalBody.AddOrAppendToCustomHeader(heading, body);
                }
            }

            journalWriter.Create(entryFilePath, frontMatter, journalBody.ToString());
        }