Browse Source

add some template helper functions for text elements (#2312)

pull/2313/head
Jason Dove 3 days ago committed by GitHub
parent
commit
8cc0d30c0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      ErsatzTV.Core/Metadata/EpgProgrammeTemplateData.cs
  2. 4
      ErsatzTV.Infrastructure/Data/Repositories/TemplateDataRepository.cs
  3. 1
      ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj
  4. 39
      ErsatzTV.Infrastructure/Streaming/TextElement.cs

4
ErsatzTV.Core/Metadata/EpgProgrammeTemplateData.cs

@ -2,8 +2,8 @@ namespace ErsatzTV.Core.Metadata; @@ -2,8 +2,8 @@ namespace ErsatzTV.Core.Metadata;
public class EpgProgrammeTemplateData
{
public DateTime Start { get; set; }
public DateTime Stop { get; set; }
public DateTimeOffset Start { get; set; }
public DateTimeOffset Stop { get; set; }
public string Title { get; set; }
public string SubTitle { get; set; }
public string Description { get; set; }

4
ErsatzTV.Infrastructure/Data/Repositories/TemplateDataRepository.cs

@ -56,7 +56,7 @@ public class TemplateDataRepository(ILocalFileSystem localFileSystem, IDbContext @@ -56,7 +56,7 @@ public class TemplateDataRepository(ILocalFileSystem localFileSystem, IDbContext
DateTimeStyles.None,
out var start))
{
data.Start = start.LocalDateTime;
data.Start = start;
}
if (DateTimeOffset.TryParseExact(
@ -66,7 +66,7 @@ public class TemplateDataRepository(ILocalFileSystem localFileSystem, IDbContext @@ -66,7 +66,7 @@ public class TemplateDataRepository(ILocalFileSystem localFileSystem, IDbContext
DateTimeStyles.None,
out var stop))
{
data.Stop = stop.LocalDateTime;
data.Stop = stop;
}
result.Add(data);

1
ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj

@ -40,6 +40,7 @@ @@ -40,6 +40,7 @@
<!-- transitive; upgrading for vuln -->
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.11" />
<PackageReference Include="TimeZoneConverter" Version="7.0.0" />
</ItemGroup>
<ItemGroup>

39
ErsatzTV.Infrastructure/Streaming/TextElement.cs

@ -1,13 +1,16 @@ @@ -1,13 +1,16 @@
using System.Globalization;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Graphics;
using Microsoft.Extensions.Logging;
using NCalc;
using Scriban;
using Scriban.Runtime;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using TimeZoneConverter;
using Image=SixLabors.ImageSharp.Image;
namespace ErsatzTV.Infrastructure.Streaming;
@ -45,7 +48,41 @@ public class TextElement(TextGraphicsElement textElement, Dictionary<string, obj @@ -45,7 +48,41 @@ public class TextElement(TextGraphicsElement textElement, Dictionary<string, obj
ZIndex = textElement.ZIndex ?? 0;
string textToRender = await Template.Parse(textElement.Text).RenderAsync(variables, memberRenamer: member => member.Name);
var scriptObject = new ScriptObject();
scriptObject.Import(variables, renamer: member => member.Name);
scriptObject.Import("convert_timezone", new Func<DateTimeOffset, string, DateTimeOffset>((dt, tzId) =>
{
try
{
var tz = TZConvert.GetTimeZoneInfo(tzId);
return TimeZoneInfo.ConvertTime(dt, tz);
}
catch (TimeZoneNotFoundException ex)
{
logger.LogWarning(ex, "Exception finding specified time zone; resulting time will be unchanged");
return dt;
}
}));
scriptObject.Import("format_datetime", new Func<DateTimeOffset, string, string, string>((dt, tzId, format) =>
{
try
{
var tz = TZConvert.GetTimeZoneInfo(tzId);
dt = TimeZoneInfo.ConvertTime(dt, tz);
}
catch (TimeZoneNotFoundException ex)
{
logger.LogWarning(ex, "Exception finding specified time zone; resulting time will be unchanged");
}
return dt.ToString(format, CultureInfo.CurrentCulture);
}));
var context = new TemplateContext { MemberRenamer = member => member.Name };
context.PushGlobal(scriptObject);
string textToRender = await Template.Parse(textElement.Text).RenderAsync(context);
var font = GraphicsEngineFonts.GetFont(textElement.FontFamily, textElement.FontSize ?? 48,
FontStyle.Regular);

Loading…
Cancel
Save