53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using ln.templates.html;
|
|
|
|
namespace ln.templates
|
|
{
|
|
public class FileSystemTemplateSource : ITemplateResolver
|
|
{
|
|
public String BasePath { get; }
|
|
public bool Throws { get; }
|
|
|
|
Dictionary<string, Template> _templates = new Dictionary<string, Template>();
|
|
|
|
public FileSystemTemplateSource(String path,bool throws)
|
|
:this(path)
|
|
{
|
|
Throws = throws;
|
|
}
|
|
public FileSystemTemplateSource(String path)
|
|
{
|
|
path = Path.GetFullPath(path);
|
|
|
|
if (!Directory.Exists(path))
|
|
throw new FileNotFoundException();
|
|
BasePath = path;
|
|
}
|
|
|
|
public Template GetTemplateByPath(string path) => GetTemplateByPath(path, Throws);
|
|
public Template GetTemplateByPath(string path,bool _throw)
|
|
{
|
|
string templatePath = Path.Combine(BasePath, path);
|
|
if (!File.Exists(templatePath))
|
|
{
|
|
if (_templates.ContainsKey(path))
|
|
_templates.Remove(path);
|
|
|
|
if (_throw)
|
|
throw new FileNotFoundException();
|
|
return null;
|
|
}
|
|
|
|
if (!_templates.TryGetValue(path,out Template template))
|
|
{
|
|
template = new Template(templatePath, this);
|
|
_templates[path] = template;
|
|
}
|
|
|
|
return template;
|
|
}
|
|
}
|
|
}
|