BadScript 2
Loading...
Searching...
No Matches
BadImportTemplateNodeTransformer.cs
Go to the documentation of this file.
1using System;
2
5
6using HtmlAgilityPack;
7
8namespace BadHtml.Transformer;
9
14{
16 protected override bool CanTransform(BadHtmlContext context)
17 {
18 return context.InputNode.Name == "bs:template";
19 }
20
22 protected override void TransformNode(BadHtmlContext context)
23 {
24 HtmlAttribute? pathAttribute = context.InputNode.Attributes["path"];
25 HtmlAttribute? modelAttribute = context.InputNode.Attributes["model"];
26
27 if (pathAttribute == null)
28 {
30 context.ExecutionContext.Scope,
31 "Missing 'path' attribute in 'bs:template' node",
32 context.CreateOuterPosition()
33 );
34 }
35
36 string? model = modelAttribute?.Value;
37
38 if (string.IsNullOrEmpty(model))
39 {
40 Console.WriteLine("Missing 'model' attribute in 'bs:template' node");
41 }
42
43 string? path = pathAttribute.Value;
44
45 if (string.IsNullOrEmpty(path))
46 {
48 context.ExecutionContext.Scope,
49 "Empty 'path' attribute in 'bs:template' node",
50 context.CreateAttributePosition(pathAttribute)
51 );
52 }
53
54 BadObject modelObj = model == null ? BadObject.Null : context.ParseAndExecuteSingle(model, context.CreateAttributePosition(modelAttribute!));
55
56 BadHtmlTemplate template = BadHtmlTemplate.Create(path, context.FileSystem);
57 HtmlDocument res = template.RunTemplate(modelObj, context.Options);
58
59 context.InputNode.AppendChildren(res.DocumentNode.ChildNodes);
60 }
61}
Implements the Html Context for the Transformation Process.
IFileSystem FileSystem
The Filesystem of the Current Template Context.
BadSourcePosition CreateAttributePosition(HtmlAttribute attribute)
Creates the Source Position of the specified Attribute.
readonly BadHtmlTemplateOptions Options
The Html Template Options.
BadSourcePosition CreateOuterPosition()
Creates the Source Position of the current Input Nodes Outer Content.
readonly HtmlNode InputNode
The Current Input Node.
readonly BadExecutionContext ExecutionContext
The Execution Context that is used to evaluate badscript code.
BadObject ParseAndExecuteSingle(string code, BadSourcePosition pos)
Parses and executes the specified code and returns the result of the last expression.
Base class of all Node transformers.
Implements a Html Template.
static BadHtmlTemplate Create(string file, IFileSystem? fileSystem=null)
Creates a new Template.
HtmlDocument RunTemplate(object? model=null, BadHtmlTemplateOptions? options=null, BadScope? caller=null)
Runs the Template with the specified arguments.
Imports the template specified in the path attribute of the current bs:template node.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implementations of Html Node Transformers that are used in the Transformation Process.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10