<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using EnumsNET;
using Microsoft.Extensions.FileProviders;

namespace SJP.Schematic.Reporting.Html
{
    internal class TemplateProvider : ITemplateProvider
    {
        public string GetTemplate(ReportTemplate template)
        {
            if (!template.IsValid())
                throw new ArgumentException($"The { nameof(ReportTemplate) } provided must be a valid enum.", nameof(template));

            var resource = GetResource(template);
            return GetResourceAsString(resource);
        }

        private static IFileInfo GetResource(ReportTemplate template)
        {
            var templateKey = template.ToString();
            var templateFileName = templateKey + TemplateExtension;

            var resourceFiles = _fileProvider.GetDirectoryContents("/");
            var templateResource = resourceFiles.FirstOrDefault(r => r.Name.EndsWith(templateFileName));
            if (templateResource == null)
                throw new NotSupportedException($"The given template: { templateKey } is not a supported template.");

            return templateResource;
        }

        private static string GetResourceAsString(IFileInfo fileInfo)
        {
            if (fileInfo == null)
                throw new ArgumentNullException(nameof(fileInfo));

            using (var stream = new MemoryStream())
            using (var reader = fileInfo.CreateReadStream())
            {
                reader.CopyTo(stream);
                return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
            }
        }

        private static readonly IFileProvider _fileProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name + ".Html.Templates");
        private const string TemplateExtension = ".cshtml";
    }
}<commit_msg>Clean up reading templates from embedded resources.<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using EnumsNET;
using Microsoft.Extensions.FileProviders;

namespace SJP.Schematic.Reporting.Html
{
    internal class TemplateProvider : ITemplateProvider
    {
        public string GetTemplate(ReportTemplate template)
        {
            if (!template.IsValid())
                throw new ArgumentException($"The { nameof(ReportTemplate) } provided must be a valid enum.", nameof(template));

            var resource = GetResource(template);
            return GetResourceAsString(resource);
        }

        private static IFileInfo GetResource(ReportTemplate template)
        {
            var templateKey = template.ToString();
            var templateFileName = templateKey + TemplateExtension;

            var resourceFiles = _fileProvider.GetDirectoryContents("/");
            var templateResource = resourceFiles.FirstOrDefault(r => r.Name.EndsWith(templateFileName));
            if (templateResource == null)
                throw new NotSupportedException($"The given template: { templateKey } is not a supported template.");

            return templateResource;
        }

        private static string GetResourceAsString(IFileInfo fileInfo)
        {
            if (fileInfo == null)
                throw new ArgumentNullException(nameof(fileInfo));

            using (var stream = fileInfo.CreateReadStream())
            using (var reader = new StreamReader(stream))
                return reader.ReadToEnd();
        }

        private static readonly IFileProvider _fileProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name + ".Html.Templates");
        private const string TemplateExtension = ".cshtml";
    }
}
{'fraction_non_alphanumeric': 0.08557844690966719, 'fraction_numerical': 0.0005282620179609086, 'mean_word_length': 2.7054794520547945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4327900', 'n_tokens_mistral': 890, 'n_tokens_neox': 871, 'n_words': 233}