Skip to content

Commit

Permalink
Added support for path expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
grumpydev committed Jul 29, 2011
1 parent 3041a22 commit 46f3985
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
11 changes: 11 additions & 0 deletions SuperSimpleViewEngine.Tests/FakeViewEngineHost.cs
Expand Up @@ -5,6 +5,7 @@ namespace SuperSimpleViewEngine.Tests
public class FakeViewEngineHost : IViewEngineHost
{
public Func<string, object, string> GetTemplateCallback { get; set; }
public Func<string, string> ExpandPathCallBack { get; set; }

/// <summary>
/// Html "safe" encode a string
Expand Down Expand Up @@ -40,5 +41,15 @@ public string GetUriString(string name, params string[] parameters)
{
throw new NotImplementedException();
}

/// <summary>
/// Expands a path to include any base paths
/// </summary>
/// <param name="path">Path to expand</param>
/// <returns>Expanded path</returns>
public string ExpandPath(string path)
{
return this.ExpandPathCallBack != null ? this.ExpandPathCallBack.Invoke(path) : path;
}
}
}
14 changes: 13 additions & 1 deletion SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs
Expand Up @@ -653,13 +653,25 @@ public void Should_handle_master_page_hierarchies()

var fakeViewEngineHost = new FakeViewEngineHost();
fakeViewEngineHost.GetTemplateCallback = (s, m) => s == "middle" ? middle : top;

var viewEngine = new SuperSimpleViewEngine(fakeViewEngineHost);

var result = viewEngine.Render(input, null);

Assert.Equal("Top! Top\r\nMiddle", result);
}

[Fact]
public void Should_call_to_expand_paths()
{
const string input = @"<script src='@Path['~/scripts/test.js']'></script>";
var fakeViewEngineHost = new FakeViewEngineHost();
fakeViewEngineHost.ExpandPathCallBack = s => s.Replace("~/", "/BasePath/");
var viewEngine = new SuperSimpleViewEngine(fakeViewEngineHost);

var result = viewEngine.Render(input, null);

Assert.Equal("<script src='/BasePath/scripts/test.js'></script>", result);
}
}

public class User
Expand Down
7 changes: 7 additions & 0 deletions SuperSimpleViewEngine/IViewEngineHost.cs
Expand Up @@ -28,5 +28,12 @@ public interface IViewEngineHost
/// <param name="parameters">Parameters to use to expand the uri string</param>
/// <returns>Expanded uri string, or null if not found</returns>
string GetUriString(string name, params string[] parameters);

/// <summary>
/// Expands a path to include any base paths
/// </summary>
/// <param name="path">Path to expand</param>
/// <returns>Expanded path</returns>
string ExpandPath(string path);
}
}
28 changes: 28 additions & 0 deletions SuperSimpleViewEngine/SuperSimpleViewEngine.cs
Expand Up @@ -54,6 +54,11 @@ public class SuperSimpleViewEngine
/// </summary>
private readonly Regex masterPageHeaderRegEx = new Regex(@"^(?:@Master\[\'(?<MasterPage>.+?)\'\]);?", RegexOptions.Compiled);

/// <summary>
/// Compiled RegEx for path expansion
/// </summary>
private readonly Regex pathExpansionRegEx = new Regex(@"(?:@Path\[\'(?<Path>.+?)\'\]);?", RegexOptions.Compiled);

/// <summary>
/// View engine transform processors
/// </summary>
Expand All @@ -76,6 +81,7 @@ public SuperSimpleViewEngine(IViewEngineHost viewEngineHost)
this.PerformSingleSubstitutions,
this.PerformEachSubstitutions,
this.PerformConditionalSubstitutions,
this.PerformPathSubstitutions,
this.PerformPartialSubstitutions,
this.PerformMasterPageSubstitutions,
};
Expand Down Expand Up @@ -402,6 +408,28 @@ private string PerformConditionalSubstitutions(string template, object model)
return result;
}

/// <summary>
/// Perform path expansion substitutions
/// </summary>
/// <param name="template">The template.</param>
/// <param name="model">The model.</param>
/// <returns>Template with paths expanded</returns>
private string PerformPathSubstitutions(string template, object model)
{
var result = template;

result = this.pathExpansionRegEx.Replace(
result,
m =>
{
var path = m.Groups["Path"].Value;
return this.viewEngineHost.ExpandPath(path);
});

return result;
}

/// <summary>
/// Perform @Partial partial view expansion
/// </summary>
Expand Down

0 comments on commit 46f3985

Please sign in to comment.