<commit_before>﻿using CommonMark;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using Viewer.TemplateFormatter;
using Viewer.TemplateFormatter.HtmlRenderers;

namespace OwinSelfhostSample
{
    public class FileController : ApiController
    {
        public HttpResponseMessage Get(String file)
        {
            bool parse = true;

            if(file.EndsWith(".text", StringComparison.InvariantCultureIgnoreCase))
            {
                file = file.Substring(0, file.Length - 5);
                parse = false;
            }

            if (!Path.GetExtension(file).Equals(".md", StringComparison.InvariantCultureIgnoreCase))
            {
                return statusCodeResponse(HttpStatusCode.NotFound);
            }

            var identifier = new DefaultHtmlTagIdentiifer();
            var renderers = new TemplatedHtmlRenderer();
            renderers.openDoc("template.html");
            var tagMap = new HtmlTagMap(renderers.getRenderer);
            CommonMarkSettings.Default.OutputDelegate = (doc, output, settings) => new FileTemplateHtmlFormatter(tagMap, identifier, output, settings).WriteDocument(doc);

            try
            {
                String content;
                using (var reader = new StreamReader(file))
                {
                    if(parse)
                    {
                        using (var writer = new StringWriter())
                        {
                            CommonMarkConverter.Convert(reader, writer);
                            content = writer.ToString();
                        }
                    }
                    else
                    {
                        content = $"<html><head></head><body><pre>{HttpUtility.HtmlEncode(reader.ReadToEnd())}</pre></body></html>";
                    }
                }

                return htmlResponse(content);
            }
            catch(FileNotFoundException)
            {<commit_msg>Changed the way files are handled.<commit_after>﻿using CommonMark;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using Viewer.TemplateFormatter;
using Viewer.TemplateFormatter.HtmlRenderers;

namespace OwinSelfhostSample
{
    public class FileController : ApiController
    {
        public HttpResponseMessage Get(String file)
        {
            bool parse = true;

            if(file.EndsWith(".text", StringComparison.InvariantCultureIgnoreCase))
            {
                file = file.Substring(0, file.Length - 5) + ".md";
                parse = false;
            }
            else if (!Path.GetExtension(file).Equals(".md", StringComparison.InvariantCultureIgnoreCase))
            {
                var autoFile = file + ".md";
                if (File.Exists(autoFile))
                {
                    file = autoFile;
                }
                else
                {
                    return statusCodeResponse(HttpStatusCode.NotFound);
                }
            }

            var identifier = new DefaultHtmlTagIdentiifer();
            var renderers = new TemplatedHtmlRenderer();
            renderers.openDoc("template.html");
            var tagMap = new HtmlTagMap(renderers.getRenderer);
            CommonMarkSettings.Default.OutputDelegate = (doc, output, settings) => new FileTemplateHtmlFormatter(tagMap, identifier, output, settings).WriteDocument(doc);

            try
            {
                String content;
                using (var reader = new StreamReader(file))
                {
                    if(parse)
                    {
                        using (var writer = new StringWriter())
                        {
                            CommonMarkConverter.Convert(reader, writer);
                            content = writer.ToString();
                        }
                    }
                    else
                    {
                        content = $"<html><head></head><body><pre>{HttpUtility.HtmlEncode(reader.ReadToEnd())}</pre></body></html>";
                    }
                }

                return htmlResponse(content);
            }
            catch(FileNotFoundException)
            {
{'fraction_non_alphanumeric': 0.09105322763306908, 'fraction_numerical': 0.0009060022650056625, 'mean_word_length': 1.5062429057888762, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '1273261', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1000, 'n_words': 204}