Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/NJsonSchema.CodeGeneration/DefaultTemplateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public partial class DefaultTemplateFactory : ITemplateFactory
{
private readonly CodeGeneratorSettingsBase _settings;
private readonly Assembly[] _assemblies;
private readonly Func<string, string, string, string> _templateContentLoader;

/// <summary>Initializes a new instance of the <see cref="DefaultTemplateFactory"/> class.</summary>
/// <param name="settings">The settings.</param>
Expand All @@ -32,6 +33,7 @@ public DefaultTemplateFactory(CodeGeneratorSettingsBase settings, Assembly[] ass
{
_settings = settings;
_assemblies = assemblies;
_templateContentLoader = GetLiquidTemplate;
}

/// <summary>Creates a template for the given language, template name and template model.</summary>
Expand All @@ -45,7 +47,7 @@ public ITemplate CreateTemplate(string language, string template, object model)
return new LiquidTemplate(
language,
template,
GetLiquidTemplate,
_templateContentLoader,
model,
GetToolchainVersion(),
_settings);
Expand Down Expand Up @@ -146,15 +148,18 @@ static LiquidTemplate()
private const string TabCountRegexString = @"(\s*)?\{%(-)?\s+template\s+([a-zA-Z0-9_.]+)(\s*?.*?)\s(-)?%}";
private const string CsharpDocsRegexString = "(\n( )*)([^\n]*?) \\| csharpdocs }}";
private const string TabRegexString = "(\n( )*)([^\n]*?) \\| tab }}";
private const string EmptyTemplateCleanupRegexString = @"^[ ]+__EMPTY-TEMPLATE__$[\n]{0,1}";

#if NET8_0_OR_GREATER
private static readonly Regex _tabCountRegex = TabCountRegex();
private static readonly Regex _csharpDocsRegex = CsharpDocsRegex();
private static readonly Regex _tabRegex = TabRegex();
private static readonly Regex _emptyTemplateCleanupRegex = EmptyTemplateCleanupRegex();
#else
private static readonly Regex _tabCountRegex = new(TabCountRegexString, RegexOptions.Singleline | RegexOptions.Compiled);
private static readonly Regex _csharpDocsRegex = new(CsharpDocsRegexString, RegexOptions.Singleline | RegexOptions.Compiled);
private static readonly Regex _tabRegex = new(TabRegexString, RegexOptions.Singleline | RegexOptions.Compiled);
private static readonly Regex _emptyTemplateCleanupRegex = new(EmptyTemplateCleanupRegexString, RegexOptions.Multiline | RegexOptions.Compiled);
#endif

public LiquidTemplate(
Expand Down Expand Up @@ -243,7 +248,7 @@ public string Render()
var trimmed = render.Replace("\r", "").Trim('\n');

// clean up cases where we have called template but it produces empty output
var withoutEmptyWhiteSpace = Regex.Replace(trimmed, @"^[ ]+__EMPTY-TEMPLATE__$[\n]{0,1}", string.Empty, RegexOptions.Multiline);
var withoutEmptyWhiteSpace = _emptyTemplateCleanupRegex.Replace(trimmed, string.Empty);

// just to make sure we don't leak out marker
return withoutEmptyWhiteSpace.Replace("__EMPTY-TEMPLATE__", "");
Expand Down Expand Up @@ -275,6 +280,9 @@ public string Render()

[GeneratedRegex(TabRegexString, RegexOptions.Compiled | RegexOptions.Singleline)]
private static partial Regex TabRegex();

[GeneratedRegex(EmptyTemplateCleanupRegexString, RegexOptions.Compiled | RegexOptions.Multiline)]
private static partial Regex EmptyTemplateCleanupRegex();
#endif
}

Expand Down