Created
January 30, 2013 18:19
-
-
Save jonnii/4675384 to your computer and use it in GitHub Desktop.
Ember Cassette pipeline transformer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmberPipelineModifier : IBundlePipelineModifier<HtmlTemplateBundle> | |
{ | |
public IBundlePipeline<HtmlTemplateBundle> Modify(IBundlePipeline<HtmlTemplateBundle> pipeline) | |
{ | |
pipeline.Add(new EmberCompatabilityProcessor()); | |
return pipeline; | |
} | |
} | |
public class EmberCompatabilityProcessor : IBundleProcessor<HtmlTemplateBundle> | |
{ | |
public void Process(HtmlTemplateBundle bundle) | |
{ | |
foreach (var asset in bundle.Assets) | |
{ | |
asset.AddAssetTransformer(new EmberCompatibleIdAssetTransformer()); | |
} | |
} | |
} | |
public class EmberCompatibleIdAssetTransformer : IAssetTransformer | |
{ | |
public Func<Stream> Transform(Func<Stream> openSourceStream, IAsset asset) | |
{ | |
return () => | |
{ | |
using (var reader = new StreamReader(openSourceStream())) | |
{ | |
var content = reader.ReadToEnd(); | |
var matches = Regex.Matches(content, "id=\"([a-zA-Z-]+)\""); | |
foreach (Match match in matches) | |
{ | |
var replaced = match.Value.Replace("-", "/"); | |
content = content.Replace(match.Value, replaced); | |
} | |
return new MemoryStream(Encoding.Default.GetBytes(content)); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment