Repository navigation
Expand file tree
/
Copy pathMinifiers.cs
More file actions
66 lines (55 loc) · 1.95 KB
/
Copy pathMinifiers.cs
File metadata and controls
66 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Text.RegularExpressions;
namespace ServiceStack.Html
{
[Flags]
public enum Minify
{
JavaScript = 1 << 0,
Css = 1 << 1,
Html = 1 << 2,
HtmlAdvanced = 1 << 3,
}
public static class Minifiers
{
public static ICompressor JavaScript = new JSMinifierFactory();
public static ICompressor Css = new CssMinifier();
public static ICompressor Html = new HtmlCompressor();
public static ICompressor HtmlAdvanced = new HtmlCompressor
{
JavaScriptCompressor = JavaScript,
CompressJavaScript = true,
CssCompressor = Css,
CompressCss = true,
};
}
class JSMinifierFactory : ICompressor
{
public string Compress(string source) => new JSMinifier().Compress(source);
}
public class BasicHtmlMinifier : ICompressor
{
static Regex BetweenScriptTagsRegEx = new Regex(@"<script[^>]*>[\w|\t|\r|\W]*?</script>", RegexOptions.Compiled);
static Regex BetweenTagsRegex = new Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}|(?=[\r])\s{2,}", RegexOptions.Compiled);
static Regex MatchBodyRegEx = new Regex(@"</body>", RegexOptions.Compiled);
public static string MinifyHtml(string html)
{
if (html == null)
return html;
var matches = BetweenScriptTagsRegEx.Matches(html);
html = BetweenScriptTagsRegEx.Replace(html, string.Empty);
html = BetweenTagsRegex.Replace(html, string.Empty);
var str = string.Empty;
foreach (Match match in matches)
{
str += match.ToString();
}
html = MatchBodyRegEx.Replace(html, str + "</body>");
return html;
}
public string Compress(string html)
{
return MinifyHtml(html);
}
}
}