diff --git a/Src/Axuno.TextTemplating.Tests/Axuno.TextTemplating.Tests.csproj b/Src/Axuno.TextTemplating.Tests/Axuno.TextTemplating.Tests.csproj index 616153a..9c53a86 100644 --- a/Src/Axuno.TextTemplating.Tests/Axuno.TextTemplating.Tests.csproj +++ b/Src/Axuno.TextTemplating.Tests/Axuno.TextTemplating.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net6.0 true ..\Axuno.TextTemplating\Axuno.TextTemplating.snk axuno gGmbH @@ -17,10 +17,10 @@ - - - - + + + + diff --git a/Src/Axuno.TextTemplating.Tests/DirectoryLocator.cs b/Src/Axuno.TextTemplating.Tests/DirectoryLocator.cs index e1999b6..974976b 100644 --- a/Src/Axuno.TextTemplating.Tests/DirectoryLocator.cs +++ b/Src/Axuno.TextTemplating.Tests/DirectoryLocator.cs @@ -2,7 +2,6 @@ using System.IO; using System.Reflection; -#nullable enable namespace Axuno.TextTemplating.Tests; public class DirectoryLocator @@ -43,4 +42,4 @@ public static string GetTargetProjectPath(Type classFromTargetAssembly) throw new Exception($"Project root could not be located using the application root {applicationBasePath}."); } -} \ No newline at end of file +} diff --git a/Src/Axuno.TextTemplating/Axuno.TextTemplating.csproj b/Src/Axuno.TextTemplating/Axuno.TextTemplating.csproj index 379c821..d50f199 100644 --- a/Src/Axuno.TextTemplating/Axuno.TextTemplating.csproj +++ b/Src/Axuno.TextTemplating/Axuno.TextTemplating.csproj @@ -1,7 +1,7 @@  - netstandard2.1;net5.0 + netstandard2.1;net60 enable Text templating is used to dynamically render contents based on a template and a model. @@ -11,7 +11,7 @@ * You can pass arbitrary objects to the template context (beside the model) for advanced scenarios. The library is a modified version of Volo.Abp.TextTemplating 4.1 - © 2013 - 2021 Volosoft. Open source license with LGPLv3.0 + © 2013 - 2022 Volosoft. Open source license with LGPLv3.0 1.1.0 axuno gGmbH LGPL-3.0-only @@ -27,7 +27,7 @@ The library is a modified version of Volo.Abp.TextTemplating 4.1 Git TextTemplating.png https://github.com/axuno/Axuno.TextTemplating - Replaced package Scriban with Scriban.Signed 3.3.3 + Replaced package Scriban with Scriban.Signed 5.4.6 Library 1.1.0.0 1.1.0.0 @@ -49,17 +49,17 @@ The library is a modified version of Volo.Abp.TextTemplating 4.1 - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/Src/Axuno.TextTemplating/ITemplateContentProvider.cs b/Src/Axuno.TextTemplating/ITemplateContentProvider.cs index a17d328..fdf527e 100644 --- a/Src/Axuno.TextTemplating/ITemplateContentProvider.cs +++ b/Src/Axuno.TextTemplating/ITemplateContentProvider.cs @@ -1,21 +1,20 @@ using System.Threading.Tasks; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +public interface ITemplateContentProvider { - public interface ITemplateContentProvider - { - Task GetContentAsync( - string templateName, - string? cultureName = null, - bool tryDefaults = true, - bool useCurrentCultureIfCultureNameIsNull = true - ); + Task GetContentAsync( + string templateName, + string? cultureName = null, + bool tryDefaults = true, + bool useCurrentCultureIfCultureNameIsNull = true + ); - Task GetContentAsync( - TemplateDefinition templateDefinition, - string? cultureName = null, - bool tryDefaults = true, - bool useCurrentCultureIfCultureNameIsNull = true - ); - } -} \ No newline at end of file + Task GetContentAsync( + TemplateDefinition templateDefinition, + string? cultureName = null, + bool tryDefaults = true, + bool useCurrentCultureIfCultureNameIsNull = true + ); +} diff --git a/Src/Axuno.TextTemplating/ITemplateDefinitionContext.cs b/Src/Axuno.TextTemplating/ITemplateDefinitionContext.cs index 5ecfb3e..caef551 100644 --- a/Src/Axuno.TextTemplating/ITemplateDefinitionContext.cs +++ b/Src/Axuno.TextTemplating/ITemplateDefinitionContext.cs @@ -1,13 +1,12 @@ using System.Collections.Generic; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +public interface ITemplateDefinitionContext { - public interface ITemplateDefinitionContext - { - IReadOnlyList GetAll(string name); + IReadOnlyList GetAll(string name); - TemplateDefinition? Get(string name); + TemplateDefinition? Get(string name); - void Add(params TemplateDefinition[] definitions); - } -} \ No newline at end of file + void Add(params TemplateDefinition[] definitions); +} diff --git a/Src/Axuno.TextTemplating/ITemplateDefinitionManager.cs b/Src/Axuno.TextTemplating/ITemplateDefinitionManager.cs index 0781136..9ad29cd 100644 --- a/Src/Axuno.TextTemplating/ITemplateDefinitionManager.cs +++ b/Src/Axuno.TextTemplating/ITemplateDefinitionManager.cs @@ -1,12 +1,11 @@ using System.Collections.Generic; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +public interface ITemplateDefinitionManager { - public interface ITemplateDefinitionManager - { - TemplateDefinition? Get(string name); + TemplateDefinition? Get(string name); - IReadOnlyList GetAll(); - } -} \ No newline at end of file + IReadOnlyList GetAll(); +} diff --git a/Src/Axuno.TextTemplating/ITemplateDefinitionProvider.cs b/Src/Axuno.TextTemplating/ITemplateDefinitionProvider.cs index 99adf0b..c70d218 100644 --- a/Src/Axuno.TextTemplating/ITemplateDefinitionProvider.cs +++ b/Src/Axuno.TextTemplating/ITemplateDefinitionProvider.cs @@ -1,11 +1,10 @@ -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +public interface ITemplateDefinitionProvider { - public interface ITemplateDefinitionProvider - { - void PreDefine(ITemplateDefinitionContext context); + void PreDefine(ITemplateDefinitionContext context); - void Define(ITemplateDefinitionContext context); + void Define(ITemplateDefinitionContext context); - void PostDefine(ITemplateDefinitionContext context); - } -} \ No newline at end of file + void PostDefine(ITemplateDefinitionContext context); +} diff --git a/Src/Axuno.TextTemplating/ITemplateRenderer.cs b/Src/Axuno.TextTemplating/ITemplateRenderer.cs index 205c4d2..5f9264c 100644 --- a/Src/Axuno.TextTemplating/ITemplateRenderer.cs +++ b/Src/Axuno.TextTemplating/ITemplateRenderer.cs @@ -2,21 +2,20 @@ using System.Globalization; using System.Threading.Tasks; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +public interface ITemplateRenderer { - public interface ITemplateRenderer - { - /// - /// Renders a text template. - /// - /// The template name - /// An optional model object that is used in the template. - /// Culture name. Uses the if not specified - /// A dictionary which can be used to import global objects to the template - /// Returns the rendered text template. - Task RenderAsync(string templateName, - object? model = null, - string? cultureName = null, - Dictionary? globalContext = null); - } + /// + /// Renders a text template. + /// + /// The template name + /// An optional model object that is used in the template. + /// Culture name. Uses the if not specified + /// A dictionary which can be used to import global objects to the template + /// Returns the rendered text template. + Task RenderAsync(string templateName, + object? model = null, + string? cultureName = null, + Dictionary? globalContext = null); } diff --git a/Src/Axuno.TextTemplating/ITypeList.cs b/Src/Axuno.TextTemplating/ITypeList.cs index ebc3da8..d8acd37 100644 --- a/Src/Axuno.TextTemplating/ITypeList.cs +++ b/Src/Axuno.TextTemplating/ITypeList.cs @@ -1,43 +1,42 @@ using System; using System.Collections.Generic; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +/// +/// A shortcut for to use object as base type. +/// +public interface ITypeList : ITypeList +{ } + +/// +/// Extends to add restriction a specific base type. +/// +/// Base Type of s in this list +public interface ITypeList : IList { /// - /// A shortcut for to use object as base type. + /// Adds a type to list. /// - public interface ITypeList : ITypeList - { } + /// Type + void Add() where T : TBaseType; /// - /// Extends to add restriction a specific base type. + /// Adds a type to list if it's not already in the list. /// - /// Base Type of s in this list - public interface ITypeList : IList - { - /// - /// Adds a type to list. - /// - /// Type - void Add() where T : TBaseType; + /// Type + bool TryAdd() where T : TBaseType; - /// - /// Adds a type to list if it's not already in the list. - /// - /// Type - bool TryAdd() where T : TBaseType; - - /// - /// Checks if a type exists in the list. - /// - /// Type - /// - bool Contains() where T : TBaseType; + /// + /// Checks if a type exists in the list. + /// + /// Type + /// + bool Contains() where T : TBaseType; - /// - /// Removes a type from list - /// - /// - void Remove() where T : TBaseType; - } -} \ No newline at end of file + /// + /// Removes a type from list + /// + /// + void Remove() where T : TBaseType; +} diff --git a/Src/Axuno.TextTemplating/TemplateContentContributorContext.cs b/Src/Axuno.TextTemplating/TemplateContentContributorContext.cs index 2b6d2e0..5366a55 100644 --- a/Src/Axuno.TextTemplating/TemplateContentContributorContext.cs +++ b/Src/Axuno.TextTemplating/TemplateContentContributorContext.cs @@ -1,25 +1,24 @@ using System; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +public class TemplateContentContributorContext { - public class TemplateContentContributorContext - { - public TemplateDefinition TemplateDefinition { get; } + public TemplateDefinition TemplateDefinition { get; } - public IServiceProvider ServiceProvider { get; } + public IServiceProvider ServiceProvider { get; } - public string? Culture { get; } + public string? Culture { get; } - public TemplateContentContributorContext( - TemplateDefinition templateDefinition, - IServiceProvider serviceProvider, - string? culture) - { - TemplateDefinition = Check.NotNull(templateDefinition, nameof(templateDefinition)); - ServiceProvider = Check.NotNull(serviceProvider, nameof(serviceProvider)); - Culture = culture; - } + public TemplateContentContributorContext( + TemplateDefinition templateDefinition, + IServiceProvider serviceProvider, + string? culture) + { + TemplateDefinition = Check.NotNull(templateDefinition, nameof(templateDefinition)); + ServiceProvider = Check.NotNull(serviceProvider, nameof(serviceProvider)); + Culture = culture; } -} \ No newline at end of file +} diff --git a/Src/Axuno.TextTemplating/TemplateDefinition.cs b/Src/Axuno.TextTemplating/TemplateDefinition.cs index b6590b1..442ea3c 100644 --- a/Src/Axuno.TextTemplating/TemplateDefinition.cs +++ b/Src/Axuno.TextTemplating/TemplateDefinition.cs @@ -1,96 +1,95 @@ using System; using System.Collections.Generic; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +public class TemplateDefinition { - public class TemplateDefinition + /// + /// CTOR. + /// + /// + /// + /// + /// + /// + public TemplateDefinition( + string? name, + Type? localizationResource = null, + bool isLayout = false, + string? layout = null, + string? defaultCultureName = null) { - /// - /// CTOR. - /// - /// - /// - /// - /// - /// - public TemplateDefinition( - string? name, - Type? localizationResource = null, - bool isLayout = false, - string? layout = null, - string? defaultCultureName = null) - { - Name = Check.NotNullOrWhiteSpace(name, nameof(name), MaxNameLength); - LocalizationResource = localizationResource; - IsLayout = isLayout; - Layout = layout; - DefaultCultureName = defaultCultureName; - Properties = new Dictionary(); - } + Name = Check.NotNullOrWhiteSpace(name, nameof(name), MaxNameLength); + LocalizationResource = localizationResource; + IsLayout = isLayout; + Layout = layout; + DefaultCultureName = defaultCultureName; + Properties = new Dictionary(); + } - /// - /// The maximum length allowed for a template name. - /// - public const int MaxNameLength = 128; + /// + /// The maximum length allowed for a template name. + /// + public const int MaxNameLength = 128; - /// - /// Gets the template name. - /// - public string Name { get; } + /// + /// Gets the template name. + /// + public string Name { get; } - /// - /// Returns if the template is a layout template, else - /// - public bool IsLayout { get; } + /// + /// Returns if the template is a layout template, else + /// + public bool IsLayout { get; } - /// - /// Gets or sets the layout to use for this template. - /// - public string? Layout { get; set; } + /// + /// Gets or sets the layout to use for this template. + /// + public string? Layout { get; set; } - /// - /// If the virtual path targets a template file, - /// if the virtual path targets a directory, which should contain template files with the culture name (e.g. "en.tpl"). - /// - public bool IsInlineLocalized { get; set; } + /// + /// If the virtual path targets a template file, + /// if the virtual path targets a directory, which should contain template files with the culture name (e.g. "en.tpl"). + /// + public bool IsInlineLocalized { get; set; } - /// - /// Gets or sets the localization resource type to use for this template. - /// - public Type? LocalizationResource { get; set; } + /// + /// Gets or sets the localization resource type to use for this template. + /// + public Type? LocalizationResource { get; set; } - /// - /// Gets the default culture name. - /// - public string? DefaultCultureName { get; } + /// + /// Gets the default culture name. + /// + public string? DefaultCultureName { get; } - /// - /// Gets or sets a key-value of the dictionary. - /// - /// The name of the property. - /// - /// Returns the value in the dictionary by given index, - /// or if the given index is not present in the dictionary. - /// - public object? this[string name] - { - get => Properties.ContainsKey(name) ? Properties[name] : null; - set => Properties[name] = value; - } + /// + /// Gets or sets a key-value of the dictionary. + /// + /// The name of the property. + /// + /// Returns the value in the dictionary by given index, + /// or if the given index is not present in the dictionary. + /// + public object? this[string name] + { + get => Properties.ContainsKey(name) ? Properties[name] : null; + set => Properties[name] = value; + } - /// - /// Can be used to get or set custom properties for this template. - /// - public Dictionary Properties { get; } + /// + /// Can be used to get or set custom properties for this template. + /// + public Dictionary Properties { get; } - /// - /// Sets a property in the dictionary. - /// This is a shortcut for nested calls on this object. - /// - public virtual TemplateDefinition WithProperty(string key, object value) - { - Properties[key] = value; - return this; - } + /// + /// Sets a property in the dictionary. + /// This is a shortcut for nested calls on this object. + /// + public virtual TemplateDefinition WithProperty(string key, object value) + { + Properties[key] = value; + return this; } -} \ No newline at end of file +} diff --git a/Src/Axuno.TextTemplating/TemplateDefinitionManager.cs b/Src/Axuno.TextTemplating/TemplateDefinitionManager.cs index ca292b0..aaea520 100644 --- a/Src/Axuno.TextTemplating/TemplateDefinitionManager.cs +++ b/Src/Axuno.TextTemplating/TemplateDefinitionManager.cs @@ -5,74 +5,73 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -namespace Axuno.TextTemplating -{ - /// - /// Temple definition manager - /// - /// - /// Inject as singleton - /// - public class TemplateDefinitionManager : ITemplateDefinitionManager - { - protected Lazy> TemplateDefinitions { get; } +namespace Axuno.TextTemplating; - protected TextTemplatingOptions Options { get; } +/// +/// Temple definition manager +/// +/// +/// Inject as singleton +/// +public class TemplateDefinitionManager : ITemplateDefinitionManager +{ + protected Lazy> TemplateDefinitions { get; } - protected IServiceProvider ServiceProvider { get; } + protected TextTemplatingOptions Options { get; } - public TemplateDefinitionManager( - IOptions options, - IServiceProvider serviceProvider) - { - ServiceProvider = serviceProvider; - Options = options.Value; + protected IServiceProvider ServiceProvider { get; } - TemplateDefinitions = - new Lazy>(CreateTextTemplateDefinitions, true); - } + public TemplateDefinitionManager( + IOptions options, + IServiceProvider serviceProvider) + { + ServiceProvider = serviceProvider; + Options = options.Value; - public virtual IReadOnlyList GetAll() - { - return TemplateDefinitions.Value.Values.ToImmutableList(); - } + TemplateDefinitions = + new Lazy>(CreateTextTemplateDefinitions, true); + } - public virtual TemplateDefinition? Get(string name) - { - return TemplateDefinitions.Value.Values.FirstOrDefault(tplDef => tplDef.Name.Equals(name)); - } + public virtual IReadOnlyList GetAll() + { + return TemplateDefinitions.Value.Values.ToImmutableList(); + } - protected virtual IDictionary CreateTextTemplateDefinitions() - { - var templates = new Dictionary(); + public virtual TemplateDefinition? Get(string name) + { + return TemplateDefinitions.Value.Values.FirstOrDefault(tplDef => tplDef.Name.Equals(name)); + } - using (var scope = ServiceProvider.CreateScope()) - { - var providers = Options - .DefinitionProviders - .Select(p => scope.ServiceProvider.GetRequiredService(p) as ITemplateDefinitionProvider) - .Where(p => !(p is null)) - .ToList(); + protected virtual IDictionary CreateTextTemplateDefinitions() + { + var templates = new Dictionary(); - var context = new TemplateDefinitionContext(templates); + using (var scope = ServiceProvider.CreateScope()) + { + var providers = Options + .DefinitionProviders + .Select(p => scope.ServiceProvider.GetRequiredService(p) as ITemplateDefinitionProvider) + .Where(p => !(p is null)) + .ToList(); - foreach (var provider in providers) - { - provider!.PreDefine(context); - } + var context = new TemplateDefinitionContext(templates); - foreach (var provider in providers) - { - provider!.Define(context); - } + foreach (var provider in providers) + { + provider!.PreDefine(context); + } - foreach (var provider in providers) - { - provider!.PostDefine(context); - } + foreach (var provider in providers) + { + provider!.Define(context); } - return templates; + foreach (var provider in providers) + { + provider!.PostDefine(context); + } } + + return templates; } -} \ No newline at end of file +} diff --git a/Src/Axuno.TextTemplating/TemplateDefinitionProvider.cs b/Src/Axuno.TextTemplating/TemplateDefinitionProvider.cs index 66775cf..b127e53 100644 --- a/Src/Axuno.TextTemplating/TemplateDefinitionProvider.cs +++ b/Src/Axuno.TextTemplating/TemplateDefinitionProvider.cs @@ -1,22 +1,21 @@  -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +/// +/// Template definition provider abstract class. +/// +/// +/// Inject derived classes as transient. +/// +public abstract class TemplateDefinitionProvider : ITemplateDefinitionProvider { - /// - /// Template definition provider abstract class. - /// - /// - /// Inject derived classes as transient. - /// - public abstract class TemplateDefinitionProvider : ITemplateDefinitionProvider + public virtual void PreDefine(ITemplateDefinitionContext context) { - public virtual void PreDefine(ITemplateDefinitionContext context) - { - } + } - public abstract void Define(ITemplateDefinitionContext context); + public abstract void Define(ITemplateDefinitionContext context); - public virtual void PostDefine(ITemplateDefinitionContext context) - { - } + public virtual void PostDefine(ITemplateDefinitionContext context) + { } } diff --git a/Src/Axuno.TextTemplating/TemplateLocalizer.cs b/Src/Axuno.TextTemplating/TemplateLocalizer.cs index d50cec9..e9029c4 100644 --- a/Src/Axuno.TextTemplating/TemplateLocalizer.cs +++ b/Src/Axuno.TextTemplating/TemplateLocalizer.cs @@ -6,59 +6,58 @@ using Scriban.Runtime; using Scriban.Syntax; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +internal class TemplateLocalizer : IScriptCustomFunction { - internal class TemplateLocalizer : IScriptCustomFunction + private readonly IStringLocalizer _localizer; + + public TemplateLocalizer(IStringLocalizer localizer) { - private readonly IStringLocalizer _localizer; + _localizer = localizer; + } - public TemplateLocalizer(IStringLocalizer localizer) - { - _localizer = localizer; - } + public object Invoke(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, + ScriptBlockStatement blockStatement) + { + return GetString(arguments); + } - public object Invoke(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, - ScriptBlockStatement blockStatement) - { - return GetString(arguments); - } + public ValueTask InvokeAsync(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, + ScriptBlockStatement blockStatement) + { + return new ValueTask(GetString(arguments)); + } - public ValueTask InvokeAsync(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, - ScriptBlockStatement blockStatement) + private string GetString(ScriptArray? arguments) + { + if (arguments is null || arguments.Count == 0) { - return new ValueTask(GetString(arguments)); + return string.Empty; } - private string GetString(ScriptArray? arguments) + var name = arguments[0]?.ToString(); + if (name == null || string.IsNullOrWhiteSpace(name)) { - if (arguments is null || arguments.Count == 0) - { - return string.Empty; - } - - var name = arguments[0]?.ToString(); - if (name == null || string.IsNullOrWhiteSpace(name)) - { - return string.Empty; - } - - var args = arguments.Skip(1).Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())).ToArray(); - return (args.Any() ? _localizer[name, args] : _localizer[name])!; + return string.Empty; } - public int RequiredParameterCount => 0; + var args = arguments.Skip(1).Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())).ToArray(); + return (args.Any() ? _localizer[name, args] : _localizer[name])!; + } + + public int RequiredParameterCount => 0; - public int ParameterCount => 0; + public int ParameterCount => 0; - public ScriptVarParamKind VarParamKind => ScriptVarParamKind.Direct; + public ScriptVarParamKind VarParamKind => ScriptVarParamKind.Direct; - public Type ReturnType => typeof(string); + public Type ReturnType => typeof(string); - public ScriptParameterInfo GetParameterInfo(int index) - { - return index == 0 - ? new ScriptParameterInfo(typeof(string), "template_name") - : new ScriptParameterInfo(typeof(object), "value"); - } + public ScriptParameterInfo GetParameterInfo(int index) + { + return index == 0 + ? new ScriptParameterInfo(typeof(string), "template_name") + : new ScriptParameterInfo(typeof(object), "value"); } } diff --git a/Src/Axuno.TextTemplating/TemplateRenderer.cs b/Src/Axuno.TextTemplating/TemplateRenderer.cs index e689ac3..b167851 100644 --- a/Src/Axuno.TextTemplating/TemplateRenderer.cs +++ b/Src/Axuno.TextTemplating/TemplateRenderer.cs @@ -7,199 +7,198 @@ using Scriban; using Scriban.Runtime; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +/// +/// Template renderer. +/// +/// +/// Inject as transient. +/// +public class TemplateRenderer : ITemplateRenderer { /// - /// Template renderer. + /// Gets the . /// - /// - /// Inject as transient. - /// - public class TemplateRenderer : ITemplateRenderer + protected readonly ITemplateContentProvider TemplateContentProvider; + /// + /// Gets the . + /// + protected readonly ITemplateDefinitionManager TemplateDefinitionManager; + /// + /// Gets the . + /// + protected readonly IStringLocalizerFactory StringLocalizerFactory; + + /// + /// Creates a new instance of a class. + /// + /// + /// + /// + public TemplateRenderer( + ITemplateContentProvider templateContentProvider, + ITemplateDefinitionManager templateDefinitionManager, + IStringLocalizerFactory stringLocalizerFactory) { - /// - /// Gets the . - /// - protected readonly ITemplateContentProvider TemplateContentProvider; - /// - /// Gets the . - /// - protected readonly ITemplateDefinitionManager TemplateDefinitionManager; - /// - /// Gets the . - /// - protected readonly IStringLocalizerFactory StringLocalizerFactory; - - /// - /// Creates a new instance of a class. - /// - /// - /// - /// - public TemplateRenderer( - ITemplateContentProvider templateContentProvider, - ITemplateDefinitionManager templateDefinitionManager, - IStringLocalizerFactory stringLocalizerFactory) - { - TemplateContentProvider = templateContentProvider; - TemplateDefinitionManager = templateDefinitionManager; - StringLocalizerFactory = stringLocalizerFactory; - } + TemplateContentProvider = templateContentProvider; + TemplateDefinitionManager = templateDefinitionManager; + StringLocalizerFactory = stringLocalizerFactory; + } - /// - /// Renders a text template. - /// - /// The template name - /// An optional model object that is used in the template. - /// Culture name. Uses the if not specified - /// A dictionary which can be used to import global objects to the template - /// Returns the rendered text template. - public virtual async Task RenderAsync( - string templateName, - object? model = null, - string? cultureName = null, - Dictionary? globalContext = null) - { - globalContext ??= new Dictionary(); - - if (cultureName == null) - { - return await RenderInternalAsync( - templateName, - globalContext, - model - ); - } - - using (new CultureSwitcher(new CultureInfo(cultureName), new CultureInfo(cultureName))) - { - return await RenderInternalAsync( - templateName, - globalContext, - model - ); - } - } + /// + /// Renders a text template. + /// + /// The template name + /// An optional model object that is used in the template. + /// Culture name. Uses the if not specified + /// A dictionary which can be used to import global objects to the template + /// Returns the rendered text template. + public virtual async Task RenderAsync( + string templateName, + object? model = null, + string? cultureName = null, + Dictionary? globalContext = null) + { + globalContext ??= new Dictionary(); - /// - /// Renders a text template. - /// - /// The template name - /// An optional model object that is used in the template. - /// A dictionary which can be used to import global objects to the template - /// Returns the rendered text template. - protected virtual async Task RenderInternalAsync( - string templateName, - Dictionary globalContext, - object? model = null) + if (cultureName == null) { - var templateDefinition = TemplateDefinitionManager.Get(templateName); - if (templateDefinition is null) throw new Exception($"Template name '{templateName}' not found."); - - var renderedContent = await RenderSingleTemplateAsync( - templateDefinition, + return await RenderInternalAsync( + templateName, globalContext, model ); - - if (templateDefinition.Layout != null) - { - globalContext["content"] = renderedContent; - renderedContent = await RenderInternalAsync( - templateDefinition.Layout, - globalContext - ); - } - - return renderedContent; } - /// - /// Renders a text template. - /// - /// The template definition - /// A dictionary which can be used to import global objects to the template - /// An optional model object that is used in the template. - /// Returns the rendered text template. - protected virtual async Task RenderSingleTemplateAsync( - TemplateDefinition templateDefinition, - Dictionary globalContext, - object? model = null) + using (new CultureSwitcher(new CultureInfo(cultureName), new CultureInfo(cultureName))) { - var rawTemplateContent = await TemplateContentProvider - .GetContentAsync( - templateDefinition - ); - - return await RenderTemplateContentWithScribanAsync( - templateDefinition, - rawTemplateContent, + return await RenderInternalAsync( + templateName, globalContext, model ); } + } + + /// + /// Renders a text template. + /// + /// The template name + /// An optional model object that is used in the template. + /// A dictionary which can be used to import global objects to the template + /// Returns the rendered text template. + protected virtual async Task RenderInternalAsync( + string templateName, + Dictionary globalContext, + object? model = null) + { + var templateDefinition = TemplateDefinitionManager.Get(templateName); + if (templateDefinition is null) throw new Exception($"Template name '{templateName}' not found."); + + var renderedContent = await RenderSingleTemplateAsync( + templateDefinition, + globalContext, + model + ); - /// - /// Renders a text template with Scriban. - /// - /// The template definition - /// The template content - /// A dictionary which can be used to import global objects to the template - /// An optional model object that is used in the template. - /// Returns the rendered text template. - protected virtual async Task RenderTemplateContentWithScribanAsync( - TemplateDefinition templateDefinition, - string? templateContent, - Dictionary globalContext, - object? model = null) + if (templateDefinition.Layout != null) { - var context = CreateScribanTemplateContext( - templateDefinition, - globalContext, - model + globalContext["content"] = renderedContent; + renderedContent = await RenderInternalAsync( + templateDefinition.Layout, + globalContext ); - - return await Template - .Parse(templateContent) - .RenderAsync(context); } - /// - /// Creates a new Scriban . - /// - /// The . - /// The global context. - /// The model to render the template. - /// - protected virtual TemplateContext CreateScribanTemplateContext( - TemplateDefinition templateDefinition, - Dictionary globalContext, - object? model = null) - { - var context = new TemplateContext(); + return renderedContent; + } - var scriptObject = new ScriptObject(); + /// + /// Renders a text template. + /// + /// The template definition + /// A dictionary which can be used to import global objects to the template + /// An optional model object that is used in the template. + /// Returns the rendered text template. + protected virtual async Task RenderSingleTemplateAsync( + TemplateDefinition templateDefinition, + Dictionary globalContext, + object? model = null) + { + var rawTemplateContent = await TemplateContentProvider + .GetContentAsync( + templateDefinition + ); - scriptObject.Import(globalContext); + return await RenderTemplateContentWithScribanAsync( + templateDefinition, + rawTemplateContent, + globalContext, + model + ); + } - if (model != null) - { - scriptObject["model"] = model; - } + /// + /// Renders a text template with Scriban. + /// + /// The template definition + /// The template content + /// A dictionary which can be used to import global objects to the template + /// An optional model object that is used in the template. + /// Returns the rendered text template. + protected virtual async Task RenderTemplateContentWithScribanAsync( + TemplateDefinition templateDefinition, + string? templateContent, + Dictionary globalContext, + object? model = null) + { + var context = CreateScribanTemplateContext( + templateDefinition, + globalContext, + model + ); + + return await Template + .Parse(templateContent) + .RenderAsync(context); + } - var localizer = GetLocalizer(templateDefinition); - scriptObject.SetValue("L", new TemplateLocalizer(localizer), true); + /// + /// Creates a new Scriban . + /// + /// The . + /// The global context. + /// The model to render the template. + /// + protected virtual TemplateContext CreateScribanTemplateContext( + TemplateDefinition templateDefinition, + Dictionary globalContext, + object? model = null) + { + var context = new TemplateContext(); - context.PushGlobal(scriptObject); - // Culture may have been changed by the CultureSwitcher of TextTemplating - context.PushCulture(CultureInfo.CurrentCulture); + var scriptObject = new ScriptObject(); - return context; - } + scriptObject.Import(globalContext); - private IStringLocalizer GetLocalizer(TemplateDefinition templateDefinition) + if (model != null) { - return StringLocalizerFactory.Create(templateDefinition.LocalizationResource ?? typeof(object)); + scriptObject["model"] = model; } + + var localizer = GetLocalizer(templateDefinition); + scriptObject.SetValue("L", new TemplateLocalizer(localizer), true); + + context.PushGlobal(scriptObject); + // Culture may have been changed by the CultureSwitcher of TextTemplating + context.PushCulture(CultureInfo.CurrentCulture); + + return context; + } + + private IStringLocalizer GetLocalizer(TemplateDefinition templateDefinition) + { + return StringLocalizerFactory.Create(templateDefinition.LocalizationResource ?? typeof(object)); } } diff --git a/Src/Axuno.TextTemplating/TextTemplatingOptions.cs b/Src/Axuno.TextTemplating/TextTemplatingOptions.cs index 2853918..4ddfdab 100644 --- a/Src/Axuno.TextTemplating/TextTemplatingOptions.cs +++ b/Src/Axuno.TextTemplating/TextTemplatingOptions.cs @@ -1,27 +1,26 @@  -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +/// +/// Class for options applied to text templating. +/// +public class TextTemplatingOptions { /// - /// Class for options applied to text templating. + /// CTOR. /// - public class TextTemplatingOptions + public TextTemplatingOptions() { - /// - /// CTOR. - /// - public TextTemplatingOptions() - { - DefinitionProviders = new TypeList(); - ContentContributors = new TypeList(); - } - - /// - /// Gets a of s. - /// - public ITypeList DefinitionProviders { get; } - /// - /// Gets a of s. - /// - public ITypeList ContentContributors { get; } + DefinitionProviders = new TypeList(); + ContentContributors = new TypeList(); } -} \ No newline at end of file + + /// + /// Gets a of s. + /// + public ITypeList DefinitionProviders { get; } + /// + /// Gets a of s. + /// + public ITypeList ContentContributors { get; } +} diff --git a/Src/Axuno.TextTemplating/TypeList.cs b/Src/Axuno.TextTemplating/TypeList.cs index 6ac6877..0fc2273 100644 --- a/Src/Axuno.TextTemplating/TypeList.cs +++ b/Src/Axuno.TextTemplating/TypeList.cs @@ -3,153 +3,152 @@ using System.Collections.Generic; using System.Reflection; -namespace Axuno.TextTemplating +namespace Axuno.TextTemplating; + +/// +/// A shortcut for to use object as base type. +/// +public class TypeList : TypeList, ITypeList +{ +} + +/// +/// Extends to add restriction a specific base type. +/// +/// Base Type of s in this list +public class TypeList : ITypeList { /// - /// A shortcut for to use object as base type. + /// Gets the count. /// - public class TypeList : TypeList, ITypeList - { - } + /// The count. + public int Count => _typeList.Count; /// - /// Extends to add restriction a specific base type. + /// Gets a value indicating whether this instance is read only. /// - /// Base Type of s in this list - public class TypeList : ITypeList - { - /// - /// Gets the count. - /// - /// The count. - public int Count => _typeList.Count; - - /// - /// Gets a value indicating whether this instance is read only. - /// - /// true if this instance is read only; otherwise, false. - public bool IsReadOnly => false; - - /// - /// Gets or sets the at the specified index. - /// - /// Index. - public Type this[int index] - { - get { return _typeList[index]; } - set - { - CheckType(value); - _typeList[index] = value; - } - } - - private readonly List _typeList; + /// true if this instance is read only; otherwise, false. + public bool IsReadOnly => false; - /// - /// Creates a new object. - /// - public TypeList() + /// + /// Gets or sets the at the specified index. + /// + /// Index. + public Type this[int index] + { + get { return _typeList[index]; } + set { - _typeList = new List(); + CheckType(value); + _typeList[index] = value; } + } - /// - public void Add() where T : TBaseType - { - _typeList.Add(typeof(T)); - } + private readonly List _typeList; - public bool TryAdd() where T : TBaseType - { - if (Contains()) - { - return false; - } + /// + /// Creates a new object. + /// + public TypeList() + { + _typeList = new List(); + } - Add(); - return true; - } + /// + public void Add() where T : TBaseType + { + _typeList.Add(typeof(T)); + } - /// - public void Add(Type item) + public bool TryAdd() where T : TBaseType + { + if (Contains()) { - CheckType(item); - _typeList.Add(item); + return false; } - /// - public void Insert(int index, Type item) - { - CheckType(item); - _typeList.Insert(index, item); - } + Add(); + return true; + } - /// - public int IndexOf(Type item) - { - return _typeList.IndexOf(item); - } + /// + public void Add(Type item) + { + CheckType(item); + _typeList.Add(item); + } - /// - public bool Contains() where T : TBaseType - { - return Contains(typeof(T)); - } + /// + public void Insert(int index, Type item) + { + CheckType(item); + _typeList.Insert(index, item); + } - /// - public bool Contains(Type item) - { - return _typeList.Contains(item); - } + /// + public int IndexOf(Type item) + { + return _typeList.IndexOf(item); + } - /// - public void Remove() where T : TBaseType - { - _typeList.Remove(typeof(T)); - } + /// + public bool Contains() where T : TBaseType + { + return Contains(typeof(T)); + } - /// - public bool Remove(Type item) - { - return _typeList.Remove(item); - } + /// + public bool Contains(Type item) + { + return _typeList.Contains(item); + } - /// - public void RemoveAt(int index) - { - _typeList.RemoveAt(index); - } + /// + public void Remove() where T : TBaseType + { + _typeList.Remove(typeof(T)); + } - /// - public void Clear() - { - _typeList.Clear(); - } + /// + public bool Remove(Type item) + { + return _typeList.Remove(item); + } - /// - public void CopyTo(Type[] array, int arrayIndex) - { - _typeList.CopyTo(array, arrayIndex); - } + /// + public void RemoveAt(int index) + { + _typeList.RemoveAt(index); + } - /// - public IEnumerator GetEnumerator() - { - return _typeList.GetEnumerator(); - } + /// + public void Clear() + { + _typeList.Clear(); + } - IEnumerator IEnumerable.GetEnumerator() - { - return _typeList.GetEnumerator(); - } + /// + public void CopyTo(Type[] array, int arrayIndex) + { + _typeList.CopyTo(array, arrayIndex); + } + + /// + public IEnumerator GetEnumerator() + { + return _typeList.GetEnumerator(); + } - private static void CheckType(Type item) + IEnumerator IEnumerable.GetEnumerator() + { + return _typeList.GetEnumerator(); + } + + private static void CheckType(Type item) + { + if (!typeof(TBaseType).GetTypeInfo().IsAssignableFrom(item)) { - if (!typeof(TBaseType).GetTypeInfo().IsAssignableFrom(item)) - { - throw new ArgumentException($"Given type ({item.AssemblyQualifiedName}) should be instance of {typeof(TBaseType).AssemblyQualifiedName} ", nameof(item)); - } + throw new ArgumentException($"Given type ({item.AssemblyQualifiedName}) should be instance of {typeof(TBaseType).AssemblyQualifiedName} ", nameof(item)); } } -} \ No newline at end of file +} diff --git a/Src/TextTemplatingDemo/TextTemplatingDemo.csproj b/Src/TextTemplatingDemo/TextTemplatingDemo.csproj index dbb3b86..2eb9165 100644 --- a/Src/TextTemplatingDemo/TextTemplatingDemo.csproj +++ b/Src/TextTemplatingDemo/TextTemplatingDemo.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1;net50 + net60 latest enable @@ -22,9 +22,9 @@ - - - + + +