Exemple #1
0
        public async Task <PreviewResult> Preview(PrintingPreviewTemplate template, PrintArguments args, CancellationToken cancellation)
        {
            await Initialize(cancellation);

            var parameters       = template.Parameters?.Select(e => new AbstractParameter(e.Key, e.Control));
            var abstractTemplate = new AbstractPrintingTemplate(template.Body, template.DownloadName, template.Context, parameters);

            var(body, downloadName) = await PrintImpl(abstractTemplate, args, cancellation);

            return(new PreviewResult(body, downloadName));
        }
Exemple #2
0
        private async Task <(string body, string fileName)> PrintImpl(AbstractPrintingTemplate template, PrintArguments args, CancellationToken cancellation)
        {
            // (1) The templates
            var nameP = new TemplatePlanLeaf(template.DownloadName, TemplateLanguage.Text);
            var bodyP = new TemplatePlanLeaf(template.Body, TemplateLanguage.Html);

            TemplatePlan plan = new TemplatePlanTuple(nameP, bodyP);

            if (!string.IsNullOrWhiteSpace(template.Context))
            {
                plan = new TemplatePlanDefine("$", template.Context, plan);
            }

            // (2) Functions + Variables
            var globalFunctions = new Dictionary <string, EvaluationFunction>();
            var localFunctions  = new Dictionary <string, EvaluationFunction>();
            var globalVariables = new Dictionary <string, EvaluationVariable>();
            var localVariables  = BaseUtil.CustomLocalVariables(args, template.Parameters?.Select(e => e.Key));

            await FactBehavior.SetPrintingFunctions(localFunctions, globalFunctions, cancellation);

            await FactBehavior.SetPrintingVariables(localVariables, globalVariables, cancellation);

            // (3) Generate output
            CultureInfo culture = GetCulture(args.Culture);
            var         genArgs = new TemplateArguments(globalFunctions, globalVariables, localFunctions, localVariables, culture: culture);
            await _templateService.GenerateFromPlan(plan : plan, args : genArgs, cancellation : cancellation);

            var downloadName = nameP.Outputs[0];
            var body         = bodyP.Outputs[0];

            // Use a default download name if none is provided
            if (string.IsNullOrWhiteSpace(downloadName))
            {
                downloadName = "File.html";
            }

            if (!downloadName.ToLower().EndsWith(".html"))
            {
                downloadName += ".html";
            }

            // Return as a file
            return(new(body, downloadName));
        }