BadScript 2
Loading...
Searching...
No Matches
BadHtmlNodeTransformer.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3
5
7
8using HtmlAgilityPack;
9
10namespace BadHtml;
11
15public abstract class BadHtmlNodeTransformer
16{
20 private static readonly List<BadHtmlNodeTransformer> s_Transformers = new List<BadHtmlNodeTransformer>
21 {
34 };
35
41 protected abstract bool CanTransform(BadHtmlContext context);
42
47 protected abstract void TransformNode(BadHtmlContext context);
48
54 protected static void TransformAttributes(BadHtmlContext context, HtmlNode outputNode)
55 {
56 foreach (HtmlAttribute attribute in context.InputNode.Attributes)
57 {
58 //Get Attribute Value
59 string value = attribute.Value;
60
61 //Replace all newlines with spaces
62 value = value.Replace("\n", " ").Replace("\r", " ");
63
64 //Evaluate Attribute Value with BadScript
65 BadObject result =
66 context.ParseAndExecute("$\"" + value + "\";", context.CreateAttributePosition(attribute));
67
68 //Set Attribute Value
69 outputNode.Attributes[attribute.Name].Value = result.ToString();
70 }
71 }
72
78 public static void Transform(BadHtmlContext context)
79 {
80 foreach (BadHtmlNodeTransformer transformer in s_Transformers)
81 {
82 if (!transformer.CanTransform(context))
83 {
84 continue;
85 }
86
87 transformer.TransformNode(context);
88
89 return;
90 }
91
92 throw new InvalidOperationException("No transformer found");
93 }
94}
Implements the Html Context for the Transformation Process.
Base class of all Node transformers.
static readonly List< BadHtmlNodeTransformer > s_Transformers
List of all node transformers used in the engine.
static void TransformAttributes(BadHtmlContext context, HtmlNode outputNode)
Transforms all attributes of the input node and writes it to the specified output node.
void TransformNode(BadHtmlContext context)
Transforms the input node.
bool CanTransform(BadHtmlContext context)
Returns true if the transformer can transform the specified node.
static void Transform(BadHtmlContext context)
Transforms the input node with one of the registered transformers.
Copies the current node to the output and transforms the attributes This is the default transformer.
Copies the current script node to the output if the lang attribute is not bs2.
Copies the current style node to the output.
Executes the current script block if the lang attribute is bs2.
Implements the 'bs:each' node transformer.
Implements a BadScript Function Node Transformer.
Implements the 'bs:if' node transformer.
Imports the file specified in the path attribute of the current bs:import node.
Imports the template specified in the path attribute of the current bs:template node.
Implements the bs:insert node transformer. That inserts the inner content of the node into the specif...
Implements a BadScript Text Node Transformer Copies the text nodes to the output and evaluates them w...
Implements the 'bs:while' node transformer.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:210
Implementations of Html Node Transformers that are used in the Transformation Process.
A Html Template Generator based on BadScript2.
Contains the Runtime Objects.
Definition BadArray.cs:10