Fixed RecursiveResolver.cs and added test case
parent
05e4b6adaf
commit
6ebd79ac21
|
@ -47,5 +47,25 @@ namespace ln.templates.test
|
||||||
Console.WriteLine("Rendered Document:\n{0}", targetWriter.ToString());
|
Console.WriteLine("Rendered Document:\n{0}", targetWriter.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase()]
|
||||||
|
public void Test_Slots_Recursive()
|
||||||
|
{
|
||||||
|
RecursiveResolver templateSource = new RecursiveResolver("tests");
|
||||||
|
|
||||||
|
foreach (var prefix in new string[] { "", "custom/option1", "custom/option2" })
|
||||||
|
{
|
||||||
|
RecursiveResolver finalResolver = templateSource.FindResolver(prefix);
|
||||||
|
StringWriter targetWriter = new StringWriter();
|
||||||
|
Template template = finalResolver.GetTemplateByPath("test_slots_recursive.html");
|
||||||
|
|
||||||
|
template.Render(targetWriter);
|
||||||
|
|
||||||
|
template = null;
|
||||||
|
Console.WriteLine("Rendered Document (prefixed: {1}):\n{0}", targetWriter.ToString(), prefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,15 @@
|
||||||
<None Update="tests\test_slots.html">
|
<None Update="tests\test_slots.html">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="tests\custom\option2\test_slot_a.html">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
<None Update="tests\custom\option1\test_slot_a.html">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
<None Update="tests\test_slots_recursive.html">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
<div style="border: 1px solid black;">
|
||||||
|
<slot name="title"><h2>Option 1</h2></slot>
|
||||||
|
</div>
|
|
@ -0,0 +1,3 @@
|
||||||
|
<div style="border: 1px solid black;">
|
||||||
|
<slot name="title"><h2>Option 2</h2></slot>
|
||||||
|
</div>
|
|
@ -0,0 +1,15 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Recursive Slot Test</h1>
|
||||||
|
<div>
|
||||||
|
<template v-include="test_slot_a.html">
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h1>This should always be the default:</h1>
|
||||||
|
<template v-include="/test_slot_a.html">
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -16,9 +16,9 @@ public class RecursiveResolver : ITemplateResolver
|
||||||
private RecursiveResolver(RecursiveResolver parent, string path)
|
private RecursiveResolver(RecursiveResolver parent, string path)
|
||||||
{
|
{
|
||||||
Parent = parent;
|
Parent = parent;
|
||||||
Path = path;
|
Path = System.IO.Path.Combine(Parent?.Path ?? "", path);
|
||||||
if (!Directory.Exists(path))
|
if (!Directory.Exists(Path))
|
||||||
throw new DirectoryNotFoundException(path);
|
throw new DirectoryNotFoundException(Path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RecursiveResolver Root => Parent?.Root ?? this;
|
public RecursiveResolver Root => Parent?.Root ?? this;
|
||||||
|
@ -32,33 +32,47 @@ public class RecursiveResolver : ITemplateResolver
|
||||||
|
|
||||||
|
|
||||||
public RecursiveResolver FindResolver(string path) => FindResolver(
|
public RecursiveResolver FindResolver(string path) => FindResolver(
|
||||||
System.IO.Path.GetDirectoryName(path)?.Split(System.IO.Path.PathSeparator) ?? Array.Empty<string>());
|
path.Split(System.IO.Path.DirectorySeparatorChar) ?? Array.Empty<string>());
|
||||||
|
|
||||||
private RecursiveResolver FindResolver(Span<string> pathElements)
|
private RecursiveResolver FindResolver(Span<string> pathElements)
|
||||||
{
|
{
|
||||||
if (pathElements.Length == 0)
|
if (pathElements.Length == 0)
|
||||||
throw new ArgumentOutOfRangeException(nameof(pathElements));
|
return this;
|
||||||
|
|
||||||
if (_children.TryGetValue(pathElements[0], out RecursiveResolver child))
|
if (!_children.TryGetValue(pathElements[0], out RecursiveResolver child))
|
||||||
{
|
{
|
||||||
if (!child.IsAlive)
|
if (Directory.Exists(System.IO.Path.Combine(Path, pathElements[0])))
|
||||||
{
|
{
|
||||||
_children.Remove(pathElements[0]);
|
child = new RecursiveResolver(this, pathElements[0]);
|
||||||
return null;
|
_children.Add(pathElements[0], child);
|
||||||
}
|
}
|
||||||
if (pathElements.Length > 1)
|
|
||||||
return child.FindResolver(pathElements.Slice(1));
|
|
||||||
return child;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (child?.IsAlive ?? false)
|
||||||
|
return child.FindResolver(pathElements.Slice(1));
|
||||||
|
|
||||||
|
if (child is not null)
|
||||||
|
_children.Remove(pathElements[0]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Template GetTemplateByPath(string templatePath)
|
public Template GetTemplateByPath(string templatePath)
|
||||||
{
|
{
|
||||||
|
RecursiveResolver resolver;
|
||||||
Span<string> pathElements = templatePath.Split(System.IO.Path.DirectorySeparatorChar);
|
Span<string> pathElements = templatePath.Split(System.IO.Path.DirectorySeparatorChar);
|
||||||
RecursiveResolver resolver = FindResolver(pathElements.Slice(0, pathElements.Length - 1));
|
if (string.Empty.Equals(pathElements[0]))
|
||||||
return resolver?.GetTemplate(pathElements[^1]);
|
resolver = Root;
|
||||||
|
else
|
||||||
|
resolver = FindResolver(pathElements.Slice(0, pathElements.Length - 1));
|
||||||
|
|
||||||
|
Template template = resolver?.GetTemplate(pathElements[^1]);
|
||||||
|
if (template is null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
template.Resolver = this;
|
||||||
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Template GetTemplate(string name)
|
public Template GetTemplate(string name)
|
||||||
|
@ -75,8 +89,14 @@ public class RecursiveResolver : ITemplateResolver
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Parent is not null)
|
||||||
|
return Parent.GetTemplate(name);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return String.Format("[RecursiveResolver {0}]", Path);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -33,7 +33,7 @@ public class Template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ITemplateResolver Resolver { get; }
|
public ITemplateResolver Resolver { get; set; }
|
||||||
public string FileName { get; }
|
public string FileName { get; }
|
||||||
public TemplateDocument Document { get; private set; }
|
public TemplateDocument Document { get; private set; }
|
||||||
public DateTime LastWriteTime { get; private set; }
|
public DateTime LastWriteTime { get; private set; }
|
||||||
|
|
Loading…
Reference in New Issue