<commit_before>﻿using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using SharpDox.Build.Loader;
using SharpDox.Build.Parser;
using SharpDox.Model.Repository;
using SharpDox.Sdk.Config;

namespace SharpDox.Build.Context.Step
{
    internal class ParseStep
    {
        private CSharpSolution _solution;
        private SDRepository _repository;
        private List<string> _excludedIdentifiers;

        private readonly SDBuildStrings _sdBuildStrings;
        private readonly BuildMessenger _buildMessenger;
        private readonly SharpDoxConfig _sharpDoxConfig;

        public ParseStep(SDBuildStrings sdBuildStrings, SharpDoxConfig sharpDoxConfig, BuildMessenger buildMessenger)
        {
            _sharpDoxConfig = sharpDoxConfig;
            _sdBuildStrings = sdBuildStrings;
            _buildMessenger = buildMessenger;
        }

        public SDRepository ParseSolution(CSharpSolution solution, List<string> excludedIdentifiers)
        {
            _solution = solution;
            _excludedIdentifiers = excludedIdentifiers ?? new List<string>();
            _repository = new SDRepository();

            _buildMessenger.ExecuteOnStepProgress(0);
            _buildMessenger.ExecuteOnBuildMessage(_sdBuildStrings.ParsingSolution);

            GetProjectInfos();
            GetImages();
            ParseNamespaces();
            ParseTypes();
            ParseArticles();
            ParseMethodCalls();
            ResolveUses();

            _buildMessenger.ExecuteOnStepProgress(100);

            return _repository;
        }

        private void GetProjectInfos()
        {
            var potentialReadMes = Directory.EnumerateFiles(Path.GetDirectoryName(_sharpDoxConfig.InputPath), "*readme*");
            if (potentialReadMes.Any())
            {
                foreach (var readme in potentialReadMes)
                {
                    var splitted = Path.GetFileName(readme).Split('.');
                    if (splitted.Length > 0 && CultureInfo.GetCultures(CultureTypes.NeutralCultures).Any(c => c.TwoLetterISOLanguageName == splitted[0].ToLower()))
                    {
                        if (!_repository.ProjectInfo.Description.ContainsKey(splitted[0].ToLower()))
                        {
                            _repository.ProjectInfo.Description.Add(splitted[0].ToLower(),
                                File.ReadAllText(readme, Encoding.Default));
                            _repository.AddDocumentationLanguage(splitted[0].ToLower());
                        }
                    }
                    else if (splitted.Length > 0 && splitted[0].ToLower() == "readme" && !_repository.ProjectInfo.Description.ContainsKey("default"))
                    {
                        _repository.ProjectInfo.Description.Add("default", File.ReadAllText(readme, Encoding.Default));
                    }
                }
            }
            else
            {
                _repository.ProjectInfo.Description.Add("default", _sharpDoxConfig.Description);
            }

            _repository.ProjectInfo.DocLanguage = _sharpDoxConfig.DocLanguage;
            _repository.ProjectInfo.LogoPath = _sharpDoxConfig.LogoPath;
            _repository.ProjectInfo.Author = _sharpDoxConfig.Author;
            _repository.ProjectInfo.ProjectName = _sharpDoxConfig.ProjectName;
            _repository.ProjectInfo.VersionNumber = _sharpDoxConfig.VersionNumber;
        }

        private void GetImages()
        {
            var images = Directory.EnumerateFiles(Path.GetDirectoryName(_sharpDoxConfig.InputPath), "sdi.*", SearchOption.AllDirectories);
            foreach (var image in images)
            {
                _repository.Images.Add(image);
            }
        }

        private void ParseArticles()
        {
            var articleParser = new ArticleParser(_sharpDoxConfig.InputPath, _repository);
            _repository.Articles = articleParser.ParseArticles();<commit_msg>A minor bugfix with the readme files<commit_after>﻿using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using SharpDox.Build.Loader;
using SharpDox.Build.Parser;
using SharpDox.Model.Repository;
using SharpDox.Sdk.Config;

namespace SharpDox.Build.Context.Step
{
    internal class ParseStep
    {
        private CSharpSolution _solution;
        private SDRepository _repository;
        private List<string> _excludedIdentifiers;

        private readonly SDBuildStrings _sdBuildStrings;
        private readonly BuildMessenger _buildMessenger;
        private readonly SharpDoxConfig _sharpDoxConfig;

        public ParseStep(SDBuildStrings sdBuildStrings, SharpDoxConfig sharpDoxConfig, BuildMessenger buildMessenger)
        {
            _sharpDoxConfig = sharpDoxConfig;
            _sdBuildStrings = sdBuildStrings;
            _buildMessenger = buildMessenger;
        }

        public SDRepository ParseSolution(CSharpSolution solution, List<string> excludedIdentifiers)
        {
            _solution = solution;
            _excludedIdentifiers = excludedIdentifiers ?? new List<string>();
            _repository = new SDRepository();

            _buildMessenger.ExecuteOnStepProgress(0);
            _buildMessenger.ExecuteOnBuildMessage(_sdBuildStrings.ParsingSolution);

            GetProjectInfos();
            GetImages();
            ParseNamespaces();
            ParseTypes();
            ParseArticles();
            ParseMethodCalls();
            ResolveUses();

            _buildMessenger.ExecuteOnStepProgress(100);

            return _repository;
        }

        private void GetProjectInfos()
        {
            var potentialReadMes = Directory.EnumerateFiles(Path.GetDirectoryName(_sharpDoxConfig.InputPath), "*readme*");
            if (potentialReadMes.Any())
            {
                foreach (var readme in potentialReadMes)
                {
                    var splitted = Path.GetFileName(readme).Split('.');
                    if (splitted.Length > 0 && CultureInfo.GetCultures(CultureTypes.NeutralCultures).Any(c => c.TwoLetterISOLanguageName == splitted[0].ToLower()))
                    {
                        if (!_repository.ProjectInfo.Description.ContainsKey(splitted[0].ToLower()))
                        {
                            _repository.ProjectInfo.Description.Add(splitted[0].ToLower(), File.ReadAllText(readme));
                            _repository.AddDocumentationLanguage(splitted[0].ToLower());
                        }
                    }
                    else if (splitted.Length > 0 && splitted[0].ToLower() == "readme" && !_repository.ProjectInfo.Description.ContainsKey("default"))
                    {
                        _repository.ProjectInfo.Description.Add("default", File.ReadAllText(readme));
                    }
                }
            }
            else
            {
                _repository.ProjectInfo.Description.Add("default", _sharpDoxConfig.Description);
            }

            _repository.ProjectInfo.DocLanguage = _sharpDoxConfig.DocLanguage;
            _repository.ProjectInfo.LogoPath = _sharpDoxConfig.LogoPath;
            _repository.ProjectInfo.Author = _sharpDoxConfig.Author;
            _repository.ProjectInfo.ProjectName = _sharpDoxConfig.ProjectName;
            _repository.ProjectInfo.VersionNumber = _sharpDoxConfig.VersionNumber;
        }

        private void GetImages()
        {
            var images = Directory.EnumerateFiles(Path.GetDirectoryName(_sharpDoxConfig.InputPath), "sdi.*", SearchOption.AllDirectories);
            foreach (var image in images)
            {
                _repository.Images.Add(image);
            }
        }

        private void ParseArticles()
        {
            var articleParser = new ArticleParser(_sharpDoxConfig.InputPath, _repository);
            _repository.Articles = articleParser.ParseArticles();
{'fraction_non_alphanumeric': 0.08351043880485061, 'fraction_numerical': 0.002750343792974122, 'mean_word_length': 2.1164783794312427, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5038318', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1951, 'n_words': 323}