<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Signum.Utilities;
using System.Web;
using Signum.Web.PortableAreas;

namespace Signum.Web.Combine
{
    public static class CssCombiner
    {
        static string Minify(string content)
        {
            content = Regex.Replace(content, @"/\*.*?\*/", "");
            content = Regex.Replace(content, "(\\s{2,}|\\t+|\\r+|\\n+)", " ");
            content = content.Replace(" {", "{");
            content = content.Replace("{ ", "{");
            content = content.Replace(" :", ":");
            content = content.Replace(": ", ":");
            content = content.Replace(", ", ",");
            content = content.Replace("; ", ";");
            content = content.Replace(";}", "}");
            content = Regex.Replace(content, "/\\*[^\\*]*\\*+([^/\\*]*\\*+)*/", "$1");
            content = Regex.Replace(content, "(?<=[>])\\s{2,}(?=[<])|(?<=[>])\\s{2,}(?=&nbsp;)|(?<=&ndsp;)\\s{2,}(?=[<])", string.Empty);

            content = Regex.Replace(content, "[^\\}]+\\{\\}", string.Empty);  //Eliminamos reglas vacías

            return content;
        }

        internal static StaticContentResult Combine(string[] virtualFiles)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("/* {0} */".Formato(virtualFiles.ToString(",")));
            foreach (var vf in virtualFiles)
            {
                var content = CombineClient.ReadStaticFile(vf);
                content = ReplaceRelativeImg(content, VirtualPathUtility.ToAbsolute(vf));
                sb.AppendLine(Minify(content));
            }

            return new StaticContentResult(Encoding.UTF8.GetBytes(sb.ToString()), "bla.css");
        }

        static string ReplaceRelativeImg(string content, string virtualFile)
        {
            string result = Regex.Replace(content, @"url\([""']?(?<route>.+?)[""']?\)", m =>
            {
                string route = m.Groups["route"].Value;

                string absolute = VirtualPathUtility.Combine(virtualFile, route); 

                return "url(\"{0}\")".Formato(absolute);
            }); 

            return result;
        }<commit_msg>Fixes ucalenda<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Signum.Utilities;
using System.Web;
using Signum.Web.PortableAreas;

namespace Signum.Web.Combine
{
    public static class CssCombiner
    {
        static string Minify(string content)
        {
            content = Regex.Replace(content, @"/\*.*?\*/", "");
            content = Regex.Replace(content, "(\\s{2,}|\\t+|\\r+|\\n+)", " ");
            content = content.Replace(" {", "{");
            content = content.Replace("{ ", "{");
            content = content.Replace(" :", ":");
            content = content.Replace(": ", ":");
            content = content.Replace(", ", ",");
            content = content.Replace("; ", ";");
            content = content.Replace(";}", "}");
            content = Regex.Replace(content, "/\\*[^\\*]*\\*+([^/\\*]*\\*+)*/", "$1");
            content = Regex.Replace(content, "(?<=[>])\\s{2,}(?=[<])|(?<=[>])\\s{2,}(?=&nbsp;)|(?<=&ndsp;)\\s{2,}(?=[<])", string.Empty);

            content = Regex.Replace(content, "[^\\}]+\\{\\}", string.Empty);  //Eliminamos reglas vacías

            return content;
        }

        internal static StaticContentResult Combine(string[] virtualFiles)
        {
            StringBuilder sb = new StringBuilder();
          
            foreach (var vf in virtualFiles)
            {
                var content = CombineClient.ReadStaticFile(vf);
                content = ReplaceRelativeImg(content, VirtualPathUtility.ToAbsolute(vf));
                sb.AppendLine("/* {0} */".Formato(vf));
                //content = Minify(content); 
                sb.AppendLine(content);
            }

            return new StaticContentResult(Encoding.UTF8.GetBytes(sb.ToString()), "bla.css");
        }

        static string ReplaceRelativeImg(string content, string virtualFile)
        {
            string result = Regex.Replace(content, @"url\([""']?(?<route>.+?)[""']?\)", m =>
            {
                string route = m.Groups["route"].Value;

                string absolute = VirtualPathUtility.Combine(virtualFile, route); 

                return "url(\"{0}\")".Formato(absolute);
            }); 

            return result;
        }
{'fraction_non_alphanumeric': 0.19525065963060687, 'fraction_numerical': 0.003518029903254178, 'mean_word_length': 2.1157534246575342, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 11, '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': '5206805', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1277, 'n_words': 223}