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(object? model = null,
75 BadHtmlTemplateOptions? options = null,
76 BadScope? caller = null)
77 {
78 options ??= new BadHtmlTemplateOptions();
79 string src = GetSource();
80
81 // ReSharper disable once UseObjectOrCollectionInitializer
82 HtmlDocument input = new HtmlDocument();
83 input.OptionUseIdAttribute = true;
84 input.LoadHtml(src);
85
86 // ReSharper disable once UseObjectOrCollectionInitializer
87 HtmlDocument output = new HtmlDocument();
88 output.OptionUseIdAttribute = true;
89 output.LoadHtml("");
90
91 BadExecutionContext executionContext =
92 (options.Runtime ?? new BadRuntime()).CreateContext(Path.GetDirectoryName(m_FileSystem.GetFullPath(FilePath)
93 ) ??
94 "/"
95 );
96 executionContext.Scope.SetCaller(caller);
97
98 if (model != null)
99 {
100 BadObject mod = model as BadObject ?? BadObject.Wrap(model);
101
102 executionContext.Scope.DefineVariable("Model",
103 mod,
104 executionContext.Scope,
106 );
107 }
108
109 foreach (HtmlNode node in input.DocumentNode.ChildNodes)
110 {
111 BadHtmlContext ctx =
112 new BadHtmlContext(node, output.DocumentNode, executionContext, FilePath, src, options, m_FileSystem);
114 }
115
116 return output;
117 }
118
126 public string Run(object? model = null, BadHtmlTemplateOptions? options = null, BadScope? caller = null)
127 {
128 return RunTemplate(model, options, caller)
129 .DocumentNode.OuterHtml;
130 }
131
138 public static BadHtmlTemplate Create(string file, IFileSystem? fileSystem = null)
139 {
140 return new BadHtmlTemplate(file, fileSystem ?? BadFileSystem.Instance);
141 }
142}
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:30
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:16
void SetCaller(BadScope? caller)
Sets the Caller of the Scope.
Definition BadScope.cs:295
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:784
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