Skip to content

Commit

Permalink
Transform Cassette URLs in bundle content loaded from manifest.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdavey committed Mar 15, 2012
1 parent 1b31a7a commit a8abea2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions examples/Precompiled/Content/Site.css
Expand Up @@ -11,4 +11,10 @@ footer,
nav,
section {
display: block;
}

h1 {
background-image: url(cassette-logo.png);
background-repeat: no-repeat;
padding-top: 120px;
}
Binary file added examples/Precompiled/Content/cassette-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/Precompiled/Precompiled.csproj
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\cassette-logo.png" />
<Content Include="Global.asax" />
<Content Include="Content\Site.css" />
<Content Include="Scripts\home.index.js" />
Expand Down
6 changes: 3 additions & 3 deletions src/Cassette/Manifests/BundleManifest.cs
Expand Up @@ -33,7 +33,7 @@ public Bundle CreateBundle(CassetteSettings settings)
bundle.PageLocation = PageLocation;
if (Assets.Count > 0)
{
bundle.Assets.Add(CreateCachedBundleContent());
bundle.Assets.Add(CreateCachedBundleContent(settings));
}
AddReferencesToBundle(bundle);
AddHtmlAttributesToBundle(bundle);
Expand All @@ -50,9 +50,9 @@ void AddHtmlAttributesToBundle(Bundle bundle)

protected abstract Bundle CreateBundleCore(CassetteSettings settings);

CachedBundleContent CreateCachedBundleContent()
CachedBundleContent CreateCachedBundleContent(CassetteSettings settings)
{
return new CachedBundleContent(Content, CreateOriginalAssets());
return new CachedBundleContent(Content, CreateOriginalAssets(), settings);
}

IEnumerable<IAsset> CreateOriginalAssets()
Expand Down
22 changes: 20 additions & 2 deletions src/Cassette/Manifests/CachedBundleContent.cs
Expand Up @@ -2,7 +2,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Cassette.IO;
using Cassette.Configuration;

namespace Cassette.Manifests
{
Expand All @@ -11,12 +14,27 @@ class CachedBundleContent : IAsset
readonly byte[] content;
readonly IEnumerable<IAsset> originalAssets;

public CachedBundleContent(byte[] content, IEnumerable<IAsset> originalAssets)
public CachedBundleContent(byte[] content, IEnumerable<IAsset> originalAssets, CassetteSettings settings)
{
this.content = content;
this.content = settings.IsUsingPrecompiledManifest ? TransformUrls(content, settings.UrlModifier) : content;
this.originalAssets = originalAssets.ToArray();
}

byte[] TransformUrls(byte[] bytes, IUrlModifier urlModifier)
{
using (var memoryStream = new MemoryStream(bytes))
using (var reader = new StreamReader(memoryStream))
{
var input = reader.ReadToEnd();
var output = Regex.Replace(
input,
"<CASSETTE_URL_ROOT>(.*?)</CASSETTE_URL_ROOT>",
match => urlModifier.Modify(match.Groups[1].Value)
);
return Encoding.UTF8.GetBytes(output);
}
}

public void Accept(IBundleVisitor visitor)
{
foreach (var originalAsset in originalAssets)
Expand Down

0 comments on commit a8abea2

Please sign in to comment.