From 70f07f03cf6f4f622143e7557c18c2847acb01ae Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 17 Feb 2025 20:24:51 +0100 Subject: [PATCH] Migrated renderer to Scriban --- ZUGFeRD.Render.Demo/Application.cs | 2 +- ZUGFeRD.Render.Demo/Program.cs | 2 +- ZUGFeRD.Render/HtmlHelper.cs | 35 ----- .../InvoiceDescriptorHtmlRenderer.cs | 63 +++----- ZUGFeRD.Render/MyViewProvider.cs | 41 ----- ZUGFeRD.Render/Simple.scriban | 148 ++++++++++++++++++ ZUGFeRD.Render/ZUGFeRD.Render.csproj | 4 +- ZUGFeRD.Render/test.cshtml | 107 ------------- 8 files changed, 176 insertions(+), 226 deletions(-) delete mode 100644 ZUGFeRD.Render/HtmlHelper.cs delete mode 100644 ZUGFeRD.Render/MyViewProvider.cs create mode 100644 ZUGFeRD.Render/Simple.scriban delete mode 100644 ZUGFeRD.Render/test.cshtml diff --git a/ZUGFeRD.Render.Demo/Application.cs b/ZUGFeRD.Render.Demo/Application.cs index 98af530f..20cad9ca 100644 --- a/ZUGFeRD.Render.Demo/Application.cs +++ b/ZUGFeRD.Render.Demo/Application.cs @@ -31,7 +31,7 @@ internal class Application internal async Task RunAsync() { InvoiceDescriptor desc = InvoiceDescriptor.Load("../../../../demodata/zugferd22/zugferd_2p2_EXTENDED_Fremdwaehrung-factur-x.xml"); - string html = InvoiceDescriptorHtmlRenderer.Render(desc); + string html = await InvoiceDescriptorHtmlRenderer.RenderAsync(desc); System.IO.File.WriteAllText("output.html", html); } // !RunAsync() } diff --git a/ZUGFeRD.Render.Demo/Program.cs b/ZUGFeRD.Render.Demo/Program.cs index 973c297b..56e6c463 100644 --- a/ZUGFeRD.Render.Demo/Program.cs +++ b/ZUGFeRD.Render.Demo/Program.cs @@ -20,7 +20,7 @@ namespace s2industries.ZUGFeRD.Render.Demo { internal class Program { - static async Task Main(string[] args) + internal static async Task Main(string[] args) { Application app = new Application(); await app.RunAsync(); diff --git a/ZUGFeRD.Render/HtmlHelper.cs b/ZUGFeRD.Render/HtmlHelper.cs deleted file mode 100644 index a50bca94..00000000 --- a/ZUGFeRD.Render/HtmlHelper.cs +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Html; - -namespace s2industries.ZUGFeRD.Render -{ - public class HtmlHelper - { - public HtmlString Raw(string source) - { - return new HtmlString(source); - } - } -} diff --git a/ZUGFeRD.Render/InvoiceDescriptorHtmlRenderer.cs b/ZUGFeRD.Render/InvoiceDescriptorHtmlRenderer.cs index e5d9f1b9..952afe56 100644 --- a/ZUGFeRD.Render/InvoiceDescriptorHtmlRenderer.cs +++ b/ZUGFeRD.Render/InvoiceDescriptorHtmlRenderer.cs @@ -1,4 +1,4 @@ -/* +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -16,62 +16,47 @@ * specific language governing permissions and limitations * under the License. */ -using Microsoft.AspNetCore.Mvc.ViewFeatures; -using Microsoft.AspNetCore.Razor.Language; -using RazorLight; -using s2industries.ZUGFeRD; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; -using System.Security; -using System.Security.Permissions; -using System.Security.Policy; using System.Text; using System.Threading.Tasks; -using System.Xml; +using Scriban; namespace s2industries.ZUGFeRD.Render { public class InvoiceDescriptorHtmlRenderer { - public static string Render(InvoiceDescriptor desc) + public static async Task RenderAsync(InvoiceDescriptor invoice) { - var assembly = Assembly.GetExecutingAssembly(); - var resourceName = typeof(InvoiceDescriptorHtmlRenderer).Namespace + ".test.cshtml"; - - string templateText = ""; - using (Stream stream = assembly.GetManifestResourceStream(resourceName)) - using (StreamReader reader = new StreamReader(stream)) + string templateContent = _LoadEmbeddedResource("Simple.scriban"); + var template = Template.Parse(templateContent); + var model = new { - templateText = reader.ReadToEnd(); - } - - var engine = new RazorLightEngineBuilder() - .UseOptions(new RazorLightOptions() - { - EnableDebugMode = true - }) - .SetOperatingAssembly(typeof(InvoiceDescriptorHtmlRenderer).GetTypeInfo().Assembly) - .Build(); - - var viewModel = new { Model = desc, Html = new HtmlHelper() }; + Invoice = invoice + }; - Task resultTask = engine.CompileRenderStringAsync("test", templateText, viewModel); - resultTask.Wait(); + string result = await template.RenderAsync(model, memberRenamer: member => member.Name); + return result; + } // !RenderAsync() - return resultTask.Result; - } // !Render() + private static string _LoadEmbeddedResource(string resourceName) + { + var assembly = Assembly.GetExecutingAssembly(); + var resourcePath = assembly.GetManifestResourceNames() + .FirstOrDefault(r => r.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase)); + if (resourcePath == null) + throw new FileNotFoundException($"Die eingebettete Ressource '{resourceName}' wurde nicht gefunden."); - public static void Render(InvoiceDescriptor desc, string filename) - { - string output = Render(desc); - StreamWriter writer = File.CreateText(filename); - writer.WriteLine(output); - writer.Close(); - } // !Render() + using (var stream = assembly.GetManifestResourceStream(resourcePath)) + using (var reader = new StreamReader(stream)) + { + return reader.ReadToEnd(); + } + } // !_LoadEmbeddedResource() } } diff --git a/ZUGFeRD.Render/MyViewProvider.cs b/ZUGFeRD.Render/MyViewProvider.cs deleted file mode 100644 index 475d0dbc..00000000 --- a/ZUGFeRD.Render/MyViewProvider.cs +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using Microsoft.Extensions.FileProviders; -using Microsoft.Extensions.Primitives; - -namespace s2industries.ZUGFeRD.Render -{ - internal class MyViewProvider : IFileProvider - { - public IDirectoryContents GetDirectoryContents(string subpath) - { - throw new System.NotImplementedException(); - } - - public IFileInfo GetFileInfo(string subpath) - { - throw new System.NotImplementedException(); - } - - public IChangeToken Watch(string filter) - { - throw new System.NotImplementedException(); - } - } -} diff --git a/ZUGFeRD.Render/Simple.scriban b/ZUGFeRD.Render/Simple.scriban new file mode 100644 index 00000000..7bd0564e --- /dev/null +++ b/ZUGFeRD.Render/Simple.scriban @@ -0,0 +1,148 @@ + + + + + + Invoice {{ Invoice.InvoiceNo }} + + + +
+
+
+

Invoice {{ InvoiceNo }}

+
+
+
+
+
+
+
Buyer
+ {{ Invoice.Buyer?.Name }}
+ {{ Invoice.Buyer?.Street }}
+ {{ Invoice.Buyer?.Postcode }} {{ Invoice.Buyer?.City }}
+
+
+
+
+
+
+
Seller
+ {{ Invoice.Seller?.Name }}
+ {{ Invoice.Seller?.Street }}
+ {{ Invoice.Seller?.Postcode }} {{ Invoice.Seller?.City }}
+
+
+
+
+ +
+
+
+
+
Ship To
+ {{ Invoice.ShipTo?.Name }}
+ {{ Invoice.ShipTo?.Street }}
+ {{ Invoice.ShipTo?.Postcode }} {{ Invoice.ShipTo?.City }}
+
+
+
+
+
+
+
Ship From
+ {{ Invoice.ShipFrom?.Name }}
+ {{ Invoice.ShipFrom?.Street }}
+ {{ Invoice.ShipFrom?.Postcode }} {{ Invoice.ShipFrom?.City }}
+
+
+
+
+ +
+
+ + + + + + + + + + + + + {{ for item in Invoice.TradeLineItems }} + + + + + + + + + {{ end }} + +
ProductNoQuantityGross Unit PriceNet Unit PriceLine Total
+ {{ item.Name }} + {{ if item.Description }} +
+ {{ item.Description }} + {{ end }} +
+ {{ item.SellerAssignedID }} +
+ {{ if item.BuyerAssignedID }} + Buy assigned id: {{ item.BuyerAssignedID }} + {{ end }} +
+ {{ if item.GlobalID }} + {{ if item.GlobalID.SchemeID != "Unknown" }} + {{ item.GlobalID.SchemeID }}: {{ item.GlobalID.Id }} + {{ end }} + {{ end }} +
+ {{ item.BilledQuantity }} + {{ if item.UnitQuantity }} +
+ Unit quantity: {{ item.UnitQuantity }} + {{ end }} +
+ {{ if item.GrossUnitPrice }} + {{ item.GrossUnitPrice | math.format("N2") }} + {{ end }} + + {{ if item.NetUnitPrice }} + {{ item.NetUnitPrice | math.format("N2") }} + {{ end }} + {{ item.LineTotalAmount | math.format("N2") }}
+
+
+ +
+
+
+
Line Total
+
{{ Invoice.LineTotalAmount | math.format("N2") }}
+
Tax Total
+
{{ Invoice.TaxTotalAmount | math.format("N2") }}
+
Grand Total
+
{{ Invoice.GrandTotalAmount | math.format("N2") }}
+ + {{ if Invoice.TotalPrepaidAmount }} +
Prepaid Amount
+
{{ Invoice.TotalPrepaidAmount | math.format("N2") }}
+ {{ end }} + + {{ if Invoice.DuePayableAmount }} +
Due Payable
+
{{ Invoice.DuePayableAmount | math.format("N2") }}
+ {{ end }} +
+
+
+
+ + + diff --git a/ZUGFeRD.Render/ZUGFeRD.Render.csproj b/ZUGFeRD.Render/ZUGFeRD.Render.csproj index 6ca8ba1a..fd771f04 100644 --- a/ZUGFeRD.Render/ZUGFeRD.Render.csproj +++ b/ZUGFeRD.Render/ZUGFeRD.Render.csproj @@ -7,11 +7,11 @@ - + - + diff --git a/ZUGFeRD.Render/test.cshtml b/ZUGFeRD.Render/test.cshtml deleted file mode 100644 index e6c75963..00000000 --- a/ZUGFeRD.Render/test.cshtml +++ /dev/null @@ -1,107 +0,0 @@ - - - - - - Invoice @Model.InvoiceNo - - - -

Invoice @Model.InvoiceNo

-
-
-
-
-
-
Buyer
- @Model.Buyer?.Name
- @Model.Buyer?.Street
- @Model.Buyer?.Postcode @Model.Buyer?.City
-
-
-
-
-
-
-
Seller
- @Model.Seller?.Name
- @Model.Seller?.Street
- @Model.Seller?.Postcode @Model.Seller?.City
-
-
-
-
- -
-
- - - - - - - - - - - @foreach (s2industries.ZUGFeRD.TradeLineItem item in Model.TradeLineItems) - { - - - - - - - } - -
ProductQuantityUnit PricePrice
@item.Name - @if (!String.IsNullOrWhiteSpace(item.Description)) - { -
- @item.Description - } -
- @item.BilledQuantity - - @if (item.UnitQuantity.HasValue) - { -
- Unit quantity: @item.UnitQuantity.Value - } -
- @if (item.NetUnitPrice.HasValue) - { - @Html.Raw(item.NetUnitPrice.Value.ToString("N2")) - } - @(item.BilledQuantity * item.NetUnitPrice.Value).ToString("N2")
-
-
- -
-
-
-
Line Total
-
@Model.LineTotalAmount.Value.ToString("N2")
-
Tax Total
-
@Model.TaxTotalAmount.Value.ToString("N2")
-
Grand Total
-
@Model.GrandTotalAmount.Value.ToString("N2")
- - @if (Model.TotalPrepaidAmount.HasValue) - { -
Prepaid Amount
-
@Model.TotalPrepaidAmount.Value.ToString("N2")
- } - - @if (Model.DuePayableAmount.HasValue) - { -
Due Payable
-
@Model.DuePayableAmount.Value.ToString("N2")
- } -
-
-
-
- - -