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 { Files = new[] { args[0] } };
41 }
42
43 return base.Parse(args);
44 }
45
47 protected override Task<int> Run(BadHtmlSystemSettings settings)
48 {
49 BadRuntimeSettings.Instance.CatchRuntimeExceptions = false;
50 BadRuntimeSettings.Instance.WriteStackTraceInRuntimeErrors = true;
51
52 if (settings.Debug)
53 {
54 Runtime.UseScriptDebugger();
55 }
56
57 BadNetworkConsoleHost? host = null;
58
59 if (settings.RemotePort != -1)
60 {
61 host = new BadNetworkConsoleHost(new TcpListener(IPAddress.Any, settings.RemotePort));
62 host.Start();
63 Runtime.UseConsole(host);
64 }
65
67 {
68 Runtime = Runtime, SkipEmptyTextNodes = settings.SkipEmptyTextNodes,
69 };
70
71 BadObject? model = null;
72
73 if (!string.IsNullOrEmpty(settings.Model))
74 {
75 model = BadJson.FromJson(File.ReadAllText(settings.Model));
76 }
77
78 foreach (string file in settings.Files)
79 {
80 string outFile = Path.ChangeExtension(file, "html");
81
82 string htmlString = BadHtmlTemplate.Create(file)
83 .Run(model, opts);
84
85 int originalSize = htmlString.Length;
86
87 if (settings.Minify)
88 {
89 htmlString = htmlString.Replace("\n", " ")
90 .Replace("\r", " ")
91 .Replace("\t", " ");
92
93 while (htmlString.Contains(" "))
94 {
95 htmlString = htmlString.Replace(" ", " ");
96 }
97
98 BadConsole.WriteLine(string.Format("Minified output to {1} characters({0}%)",
99 Math.Round(htmlString.Length / (float)originalSize * 100, 2),
100 htmlString.Length
101 )
102 );
103 }
104 else
105 {
106 BadConsole.WriteLine($"Generated output {htmlString.Length} characters");
107 }
108
109 BadFileSystem.WriteAllText(outFile, htmlString);
110 }
111
112 host?.Stop();
113
114 return Task.FromResult(0);
115 }
116}
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:30
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:78
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:223
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.