BadScript 2
Loading...
Searching...
No Matches
BadHtmlTemplate.cs
Go to the documentation of this file.
1using System.IO;
2
3using BadScript2;
4using BadScript2.IO;
8
9using HtmlAgilityPack;
10
11namespace BadHtml;
12
16public class BadHtmlTemplate
17{
21 private readonly IFileSystem m_FileSystem;
22
26 private string? m_Source;
27
33 private BadHtmlTemplate(string filePath, IFileSystem fileSystem)
34 {
35 FilePath = filePath;
36 m_FileSystem = fileSystem;
37 }
38
39
43 public string FilePath { get; }
44
49 private string GetSource()
50 {
51 if (m_Source == null)
52 {
53 Reload();
54 }
55
56 return m_Source!;
57 }
58
62 public void Reload()
63 {
64 m_Source = m_FileSystem.ReadAllText(FilePath);
65 }
66
74 public HtmlDocument RunTemplate(
75 object? model = null,
76 BadHtmlTemplateOptions? options = null,
77 BadScope? caller = null)
78 {
79 options ??= new BadHtmlTemplateOptions();
80 string src = GetSource();
81
82 // ReSharper disable once UseObjectOrCollectionInitializer
83 HtmlDocument input = new HtmlDocument();
84 input.OptionUseIdAttribute = true;
85 input.LoadHtml(src);
86
87 // ReSharper disable once UseObjectOrCollectionInitializer
88 HtmlDocument output = new HtmlDocument();
89 output.OptionUseIdAttribute = true;
90 output.LoadHtml("");
91 BadExecutionContext executionContext = (options.Runtime ?? new BadRuntime()).CreateContext(Path.GetDirectoryName(m_FileSystem.GetFullPath(FilePath)) ?? "/");
92 executionContext.Scope.SetCaller(caller);
93
94 if (model != null)
95 {
96 BadObject mod = model as BadObject ?? BadObject.Wrap(model);
97 executionContext.Scope.DefineVariable(
98 "Model",
99 mod,
100 executionContext.Scope,
102 );
103 }
104
105 foreach (HtmlNode node in input.DocumentNode.ChildNodes)
106 {
107 BadHtmlContext ctx =
108 new BadHtmlContext(node, output.DocumentNode, executionContext, FilePath, src, options, m_FileSystem);
110 }
111
112 return output;
113 }
114
122 public string Run(object? model = null, BadHtmlTemplateOptions? options = null, BadScope? caller = null)
123 {
124 return RunTemplate(model, options, caller).DocumentNode.OuterHtml;
125 }
126
133 public static BadHtmlTemplate Create(string file, IFileSystem? fileSystem = null)
134 {
135 return new BadHtmlTemplate(file, fileSystem ?? BadFileSystem.Instance);
136 }
137}
Implements the Html Context for the Transformation Process.
Base class of all Node transformers.
static void Transform(BadHtmlContext context)
Transforms the input node with one of the registered transformers.
Implements a Html Template.
static BadHtmlTemplate Create(string file, IFileSystem? fileSystem=null)
Creates a new Template.
void Reload()
Reloads the Template Source.
HtmlDocument RunTemplate(object? model=null, BadHtmlTemplateOptions? options=null, BadScope? caller=null)
Runs the Template with the specified arguments.
readonly IFileSystem m_FileSystem
The Filesystem used to load the template.
BadHtmlTemplate(string filePath, IFileSystem fileSystem)
Constructs a new Template.
string GetSource()
Returns the source code of the template. Loads the source code if it is not loaded yet.
string Run(object? model=null, BadHtmlTemplateOptions? options=null, BadScope? caller=null)
Runs the Template with the specified arguments.
string? m_Source
The Source code of the Template(gets loaded on first template run)
string FilePath
The Filepath of the Template.
Options for the Html Template Engine.
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
Public interface for the filesystem abstraction of the BadScript Engine.
static IFileSystem Instance
File System implementation.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
void SetCaller(BadScope? caller)
Sets the Caller of the Scope.
Definition BadScope.cs:512
void DefineVariable(string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
Defines a new Variable in the current scope.
Definition BadScope.cs:929
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Stores Meta Information about a Property.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Defines the interface for a file system.
Definition IFileSystem.cs:7
string GetFullPath(string path)
Returns the full path of the given path.
A Html Template Generator based on BadScript2.
Contains IO Implementation for the BadScript2 Runtime.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
The main namespace for the BadScript2 Language.
Definition BadConsole.cs:6