Skip to content

Commit 7e992a5

Browse files
Added WebMarkupMin
1 parent dba4ad4 commit 7e992a5

18 files changed

+149
-81
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ local.properties
4848
x64/
4949
build/
5050
#[Bb]in/
51+
Website/[Bb]in/*.dll
5152
[Oo]bj/
5253

5354
# MSTest test Results
@@ -131,7 +132,7 @@ publish/
131132

132133
# NuGet Packages Directory
133134
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
134-
#packages/
135+
packages/
135136

136137
# Windows Azure Build Output
137138
csx

MiniBlog.sln

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2012
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.30723.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "MiniBlog", "http://localhost:36123", "{3D10EDC4-D7A0-4912-84F1-853B6E694CE5}"
57
ProjectSection(WebsiteProperties) = preProject
6-
Frontpage = "false"
78
UseIISExpress = "true"
89
TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.5"
910
Debug.AspNetCompiler.VirtualPath = "/localhost_36123"
@@ -26,6 +27,7 @@ Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "MiniBlog", "http://localhos
2627
EndProject
2728
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C6A4C914-CC97-4804-ACA1-B8A0E61CE333}"
2829
ProjectSection(SolutionItems) = preProject
30+
.gitignore = .gitignore
2931
LICENSE.md = LICENSE.md
3032
README.md = README.md
3133
EndProjectSection

Website/Web.config

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0"?>
1+
<?xml version="1.0"?>
22
<configuration>
33

44
<appSettings>
@@ -54,6 +54,11 @@
5454
<system.webServer>
5555
<urlCompression doDynamicCompression="true" doStaticCompression="true"/>
5656

57+
<modules runAllManagedModulesForAllRequests="false">
58+
<!-- Remove this to disable HTML minification-->
59+
<add name="WhitespaceModule" type="WhitespaceModule" preCondition="managedHandler" />
60+
</modules>
61+
5762
<handlers>
5863
<remove name="CommentHandler"/>
5964
<add name="CommentHandler" verb="*" type="CommentHandler" path="/comment.ashx"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System.IO;
2+
using System.Text;
3+
using System.Web;
4+
using WebMarkupMin.Core;
5+
using WebMarkupMin.Core.Minifiers;
6+
using WebMarkupMin.Core.Settings;
7+
8+
public class WhitespaceModule : IHttpModule
9+
{
10+
#region IHttpModule Members
11+
12+
void IHttpModule.Dispose()
13+
{
14+
// Nothing to dispose;
15+
}
16+
17+
void IHttpModule.Init(HttpApplication app)
18+
{
19+
app.PostRequestHandlerExecute += (o, e) => PostRequestHandlerExecute(app);
20+
}
21+
22+
#endregion
23+
24+
private void PostRequestHandlerExecute(HttpApplication app)
25+
{
26+
string contentType = app.Response.ContentType;
27+
string method = app.Request.HttpMethod;
28+
int status = app.Response.StatusCode;
29+
IHttpHandler handler = app.Context.CurrentHandler;
30+
31+
if (contentType == "text/html" && method == "GET" && status == 200 && handler != null)
32+
{
33+
app.Response.Filter = new WhitespaceFilter(app.Response.Filter, app.Request.ContentEncoding);
34+
}
35+
}
36+
37+
#region Stream filter
38+
39+
private class WhitespaceFilter : Stream
40+
{
41+
private readonly Encoding _encoding;
42+
private readonly Stream _stream;
43+
private readonly MemoryStream _cache;
44+
private readonly static HtmlMinifier _minifier = new HtmlMinifier(new HtmlMinificationSettings
45+
{
46+
WhitespaceMinificationMode = WhitespaceMinificationMode.Aggressive,
47+
RemoveRedundantAttributes = false
48+
});
49+
50+
public WhitespaceFilter(Stream sink, Encoding encoding)
51+
{
52+
_stream = sink;
53+
_encoding = encoding;
54+
_cache = new MemoryStream();
55+
}
56+
57+
#region Properites
58+
59+
public override bool CanRead
60+
{
61+
get { return true; }
62+
}
63+
64+
public override bool CanSeek
65+
{
66+
get { return true; }
67+
}
68+
69+
public override bool CanWrite
70+
{
71+
get { return true; }
72+
}
73+
74+
public override void Flush()
75+
{
76+
_stream.Flush();
77+
}
78+
79+
public override long Length
80+
{
81+
get { return 0; }
82+
}
83+
84+
private long _position;
85+
public override long Position
86+
{
87+
get { return _position; }
88+
set { _position = value; }
89+
}
90+
91+
#endregion
92+
93+
#region Methods
94+
95+
public override int Read(byte[] buffer, int offset, int count)
96+
{
97+
return _stream.Read(buffer, offset, count);
98+
}
99+
100+
public override long Seek(long offset, SeekOrigin origin)
101+
{
102+
return _stream.Seek(offset, origin);
103+
}
104+
105+
public override void SetLength(long value)
106+
{
107+
_stream.SetLength(value);
108+
}
109+
110+
public override void Write(byte[] buffer, int offset, int count)
111+
{
112+
_cache.Write(buffer, offset, count);
113+
}
114+
115+
public override void Close()
116+
{
117+
byte[] buffer = _cache.ToArray();
118+
int cacheSize = buffer.Length;
119+
120+
string original = _encoding.GetString(buffer);
121+
string result = _minifier.Minify(original).MinifiedContent;
122+
byte[] output = _encoding.GetBytes(result);
123+
124+
_stream.Write(output, 0, output.Length);
125+
_cache.Dispose();
126+
_stream.Dispose();
127+
}
128+
129+
#endregion
130+
131+
}
132+
133+
#endregion
134+
135+
}

Website/bin/AjaxMin.dll

8 KB
Binary file not shown.

Website/bin/AjaxMin.dll.refresh

2 Bytes
Binary file not shown.
148 Bytes
Binary file not shown.

Website/packages.config

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="AjaxMin" version="5.8.5172.27710" targetFramework="net45" />
3+
<package id="AjaxMin" version="5.11.5295.12309" targetFramework="net45" />
44
<package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" />
66
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
7+
<package id="WebMarkupMin.Core" version="0.9.0-beta3" targetFramework="net45" />
78
<package id="xmlrpcnet" version="3.0.0.266" targetFramework="net45" />
89
<package id="xmlrpcnet-server" version="3.0.0.266" targetFramework="net45" />
910
</packages>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

packages/AjaxMin.5.8.5172.27710/tools/net35/AjaxMin.targets

-38
This file was deleted.
Binary file not shown.
Binary file not shown.

packages/AjaxMin.5.8.5172.27710/tools/net40/AjaxMin.targets

-38
This file was deleted.
Binary file not shown.

0 commit comments

Comments
 (0)