BadScript 2
Loading...
Searching...
No Matches
BadHtmlSystem.cs
Go to the documentation of this file.
1using System.Net;
2using System.Net.Sockets;
3
4using BadHtml;
5
10using BadScript2.IO;
13
18
22public class BadHtmlSystem : BadConsoleSystem<BadHtmlSystemSettings>
23{
28 public BadHtmlSystem(BadRuntime runtime) : base(runtime) { }
29
30
32 public override string Name => "html";
33
34
36 public override object? Parse(string[] args)
37 {
38 if (args.Length == 1 && args[0] != "--help" && args[0] != "help")
39 {
40 return new BadHtmlSystemSettings
41 {
42 Files = new[]
43 {
44 args[0],
45 },
46 };
47 }
48
49 return base.Parse(args);
50 }
51
53 protected override Task<int> Run(BadHtmlSystemSettings settings)
54 {
55 BadRuntimeSettings.Instance.CatchRuntimeExceptions = false;
56 BadRuntimeSettings.Instance.WriteStackTraceInRuntimeErrors = true;
57
58
59 if (settings.Debug)
60 {
61 Runtime.UseScriptDebugger();
62 }
63
64 BadNetworkConsoleHost? host = null;
65
66 if (settings.RemotePort != -1)
67 {
68 host = new BadNetworkConsoleHost(new TcpListener(IPAddress.Any, settings.RemotePort));
69 host.Start();
70 Runtime.UseConsole(host);
71 }
72
74 {
75 Runtime = Runtime,
76 SkipEmptyTextNodes = settings.SkipEmptyTextNodes,
77 };
78
79 BadObject? model = null;
80
81 if (!string.IsNullOrEmpty(settings.Model))
82 {
83 model = BadJson.FromJson(File.ReadAllText(settings.Model));
84 }
85
86 foreach (string file in settings.Files)
87 {
88 string outFile = Path.ChangeExtension(file, "html");
89 string htmlString = BadHtmlTemplate.Create(file).Run(model, opts);
90
91 int originalSize = htmlString.Length;
92
93 if (settings.Minify)
94 {
95 htmlString = htmlString.Replace("\n", " ")
96 .Replace("\r", " ")
97 .Replace("\t", " ");
98
99 while (htmlString.Contains(" "))
100 {
101 htmlString = htmlString.Replace(" ", " ");
102 }
103
105 string.Format(
106 "Minified output to {1} characters({0}%)",
107 Math.Round(htmlString.Length / (float)originalSize * 100, 2),
108 htmlString.Length
109 )
110 );
111 }
112 else
113 {
114 BadConsole.WriteLine($"Generated output {htmlString.Length} characters");
115 }
116
117 BadFileSystem.WriteAllText(outFile, htmlString);
118 }
119
120 host?.Stop();
121
122 return Task.FromResult(0);
123 }
124}
Implements a Html Template.
static BadHtmlTemplate Create(string file, IFileSystem? fileSystem=null)
Creates a new Template.
string Run(object? model=null, BadHtmlTemplateOptions? options=null, BadScope? caller=null)
Runs the Template with the specified arguments.
Options for the Html Template Engine.
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
Wrapper class for the console abstraction.
Definition BadConsole.cs:12
static void WriteLine(string str)
Writes a string to the console and appends a newline.
Definition BadConsole.cs:76
Implements a Console System that uses a settings object of Type T.
override Task< int > Run(BadHtmlSystemSettings settings)
BadHtmlSystem(BadRuntime runtime)
Creates a new BadHtmlSystem instance.
Settings for the Html Template Engine System.
bool Debug
If Enabled, the Debugger will be attached to the process.
string Model
The Model that the templates will use.
int RemotePort
If Specified the Remote Console will be started on the specified port.
bool SkipEmptyTextNodes
Indicates if empty HTML Text nodes should be skipped.
Public interface for the filesystem abstraction of the BadScript Engine.
static void WriteAllText(string path, string contents)
Wrapper for IFileSystem.OpenWrite that creates the file if it does not exist.
Implements a Json to BadObject Converter.
Definition BadJson.cs:17
static BadObject FromJson(string s)
Converts a Json string to a BadObject.
Definition BadJson.cs:192
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
A Html Template Generator based on BadScript2.
Contains the Implementation of the Remote Console Abstraction over TCP.
Contains a Console Abstraction Layer to be able to Simulate Console Input/Output over the Network.
Definition BadConsole.cs:6
Contains the 'html' console command implementation.
Contains the debugger implementations for the BadScript2 Runtime.
Contains IO Implementation for the BadScript2 Runtime.
Contains JSON Extensions and APIs for the BadScript2 Runtime.
Definition BadJson.cs:11
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains Runtime Settings Objects.