<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace TexWrapper
{
    public interface IReportGenerator
    {
        void Create(dynamic o, FileInfo destination);
        void Create(FileInfo source, dynamic o, FileInfo destination);
    }

    public class CSTexReportGenerator : IReportGenerator
    {
        private static string texEscapeFunc = @"
            class Tex
            {
                public static string Escape(object o) { return Escape(o.ToString()); }
                public static string Escape(string text)
                {
                    var sb = new StringBuilder();
                    foreach (var c in text)
                    {
                        switch(c)
                        {
                        case '\\': sb.Append(@""{\texttt{\char`\\}""); break;
//                        case '\\': sb.Append(@""{\textbackslash}""); break;
                        case '{': sb.Append(@""\{""); break;
                        case '}': sb.Append(@""\}""); break;
                        case '<': sb.Append(@""{\textless}""); break;
                        case '>': sb.Append(@""{\textgreater}""); break;
                        case '~': sb.Append(@""{\textasciitilde}""); break;
                        case '€': sb.Append(@""{\texteuro}""); break;
                        case '_': sb.Append(@""\_""); break;
                        case '^': sb.Append(@""\hat{\text{\ }}""); break;
                        case '§': sb.Append(@""\S""); break;
                        case '$': sb.Append(@""\$""); break;
                        case '&': sb.Append(@""\&""); break;
                        case '#': sb.Append(@""\#""); break;
                        case '%': sb.Append(@""\%""); break;
                        case ' ': sb.Append(@""\ ""); break;
                        case '""': sb.Append(@""""""'""); break;
                        default: sb.Append(c); break;
                        }
//                        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
//                            sb.Append(c);
//                        else
//                            sb.Append(@""\char"""""" + ((int)c).ToString(""X"") + @"" "");
                    }
                    return sb.ToString().Replace(""\n"",@""\\"");
                }
            }";
        private IScript script_ = null;
        private FileInfo source_ = null;
        private CSScriptCompiler compiler_ = new CSScriptCompiler();
        public CSScriptCompiler Compiler { get { return this.compiler_; } }
        public FileInfo Source { get { return this.source_; } set { this.source_ = value; this.script_ = null; } }
        public TexWrapper TexWrapper { get; private set; }

        public CSTexReportGenerator()
        {
            this.TexWrapper = new TexWrapper();
            this.compiler_.AddEscapeSequenze(@"%##", @"##%");
            this.compiler_.AddEscapeSequenze(@"\verb|##", @"##|");
            this.compiler_.AddEscapeSequenze(@"\verb$##", @"##$");
            //this.compiler_.AddEscapeSequenzeRegex(@"\\verb.\#\#", @"\#\#.");
            //this.compiler_.AddEscapeSequenzeRegex(@"\\verb(?<k84>.)\#\#", @"\#\#\k<k84>");
            this.compiler_.AddEscapeSequenze(@"\begin{comment}##", @"##\end{comment}");
            this.compiler_.FunctionCode = texEscapeFunc;
        }
        public CSTexReportGenerator(FileInfo source) : this() { this.Source = source; }

        public void Create(dynamic o, FileInfo destination)
        {
            if (this.Source == null)
                throw new InvalidOperationException("You must first specify a source.");
            if (this.script_ == null)
                using (var reader = new StreamReader(this.source_.FullName))
                    this.script_ = this.compiler_.Compile(reader.ReadToEnd());
            var texcs = new FileInfo(this.source_.FullName + "cs");
            using (var writer = new StreamWriter(texcs.FullName))
                writer.Write(this.script_.Run(o));
            this.TexWrapper.Compile(texcs, destination);
            this.TexWrapper.Compile(texcs, destination); // BugFix: Always compile twice
        }

        public void Create(FileInfo source, dynamic o, FileInfo destination)
        {
            IScript script;<commit_msg>Constructor with config parameter<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace TexWrapper
{
    public interface IReportGenerator
    {
        void Create(dynamic o, FileInfo destination);
        void Create(FileInfo source, dynamic o, FileInfo destination);
    }

    public class CSTexReportGenerator : IReportGenerator
    {
        private static string texEscapeFunc = @"
            class Tex
            {
                public static string Escape(object o) { return Escape(o.ToString()); }
                public static string Escape(string text)
                {
                    var sb = new StringBuilder();
                    foreach (var c in text)
                    {
                        switch(c)
                        {
                        case '\\': sb.Append(@""{\texttt{\char`\\}""); break;
//                        case '\\': sb.Append(@""{\textbackslash}""); break;
                        case '{': sb.Append(@""\{""); break;
                        case '}': sb.Append(@""\}""); break;
                        case '<': sb.Append(@""{\textless}""); break;
                        case '>': sb.Append(@""{\textgreater}""); break;
                        case '~': sb.Append(@""{\textasciitilde}""); break;
                        case '€': sb.Append(@""{\texteuro}""); break;
                        case '_': sb.Append(@""\_""); break;
                        case '^': sb.Append(@""\hat{\text{\ }}""); break;
                        case '§': sb.Append(@""\S""); break;
                        case '$': sb.Append(@""\$""); break;
                        case '&': sb.Append(@""\&""); break;
                        case '#': sb.Append(@""\#""); break;
                        case '%': sb.Append(@""\%""); break;
                        case ' ': sb.Append(@""\ ""); break;
                        case '""': sb.Append(@""""""'""); break;
                        default: sb.Append(c); break;
                        }
//                        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
//                            sb.Append(c);
//                        else
//                            sb.Append(@""\char"""""" + ((int)c).ToString(""X"") + @"" "");
                    }
                    return sb.ToString().Replace(""\n"",@""\\"");
                }
            }";
        private IScript script_ = null;
        private FileInfo source_ = null;
        private CSScriptCompiler compiler_ = new CSScriptCompiler();
        public CSScriptCompiler Compiler { get { return this.compiler_; } }
        public FileInfo Source { get { return this.source_; } set { this.source_ = value; this.script_ = null; } }
        public TexWrapper TexWrapper { get; private set; }

        public CSTexReportGenerator() : this(null) { }
        public CSTexReportGenerator(FileInfo configFile)
        {
            if (configFile == null)
                this.TexWrapper = new TexWrapper();
            else
                this.TexWrapper = new TexWrapper(configFile);
            this.compiler_.AddEscapeSequenze(@"%##", @"##%");
            this.compiler_.AddEscapeSequenze(@"\verb|##", @"##|");
            this.compiler_.AddEscapeSequenze(@"\verb$##", @"##$");
            //this.compiler_.AddEscapeSequenzeRegex(@"\\verb.\#\#", @"\#\#.");
            //this.compiler_.AddEscapeSequenzeRegex(@"\\verb(?<k84>.)\#\#", @"\#\#\k<k84>");
            this.compiler_.AddEscapeSequenze(@"\begin{comment}##", @"##\end{comment}");
            this.compiler_.FunctionCode = texEscapeFunc;
        }

        public void Create(dynamic o, FileInfo destination)
        {
            if (this.Source == null)
                throw new InvalidOperationException("You must first specify a source.");
            if (this.script_ == null)
                using (var reader = new StreamReader(this.source_.FullName))
                    this.script_ = this.compiler_.Compile(reader.ReadToEnd());
            var texcs = new FileInfo(this.source_.FullName + "cs");
            using (var writer = new StreamWriter(texcs.FullName))
                writer.Write(this.script_.Run(o));
            this.TexWrapper.Compile(texcs, destination);
            this.TexWrapper.Compile(texcs, destination); // BugFix: Always compile twice
        }

        public void Create(FileInfo source, dynamic o, FileInfo destination)
        {
            IScript script;
{'fraction_non_alphanumeric': 0.16634863621057358, 'fraction_numerical': 0.0013469525199236727, 'mean_word_length': 1.5871080139372822, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4020814', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2285, 'n_words': 501}