Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CssxToCss.cs
1using SkiaSharp;
2using System;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
13using Waher.Script;
14
16{
21 {
25 public CssxToCss()
26 {
27 }
28
32 public string[] FromContentTypes => new string[] { CssxDecoder.DefaultContentType };
33
37 public string[] ToContentTypes => new string[] { CssCodec.DefaultContentType };
38
42 public Grade ConversionGrade => Grade.Perfect;
43
49 public async Task<bool> ConvertAsync(ConversionState State)
50 {
51 string Cssx;
52
53 using (StreamReader rd = new StreamReader(State.From))
54 {
55 Cssx = await rd.ReadToEndAsync();
56 }
57
58 string Css = await Convert(Cssx, State, State.FromFileName);
59 if (State.HasError)
60 return false;
61
62 byte[] Data = Strings.Utf8WithBom.GetBytes(Css);
63 await State.To.WriteAsync(Data, 0, Data.Length);
64 State.ToContentType += "; charset=utf-8";
65
66 return false;
67 }
68
76 public static Task<string> Convert(string Cssx, ConversionState State, string FileName)
77 {
78 return Convert(Cssx, State, State.Session, FileName, true);
79 }
80
88 public static Task<string> Convert(string Cssx, Variables Session, string FileName)
89 {
90 return Convert(Cssx, null, Session, FileName, true);
91 }
92
103 internal static Task<string> Convert(string Cssx, ConversionState State, Variables Session, string FileName, bool LockSession)
104 {
105 return Convert(Cssx, "¤", "¤", State, Session, FileName, LockSession);
106 }
107
119 internal static async Task<string> Convert(string WebContent, string StartDelimiter, string StopDelimiter, ConversionState State,
120 Variables Session, string FileName, bool PreserveVariables)
121 {
122 if (Session is null)
123 {
124 Exception ex = new ArgumentNullException("No session defined.", nameof(Session));
125
126 if (State is null)
127 throw ex;
128 else
129 {
130 State.Error = ex;
131 return null;
132 }
133 }
134
135 if (PreserveVariables)
136 Session.Push();
137 try
138 {
140 Def?.Prepare(Session);
141
142 StringBuilder Result = new StringBuilder();
143 Expression Exp;
144 int i = 0;
145 int c = WebContent.Length;
146 int j, k;
147 string Script;
148 object Value;
149
150 while (i < c)
151 {
152 j = WebContent.IndexOf(StartDelimiter, i);
153 if (j < 0)
154 break;
155
156 if (j > i)
157 Result.Append(WebContent[i..j]);
158
159 k = WebContent.IndexOf(StopDelimiter, j + 1);
160 if (k < 0)
161 break;
162
163 Script = WebContent.Substring(j + 1, k - j - 1);
164 Exp = new Expression(Script, FileName);
165 Value = await Exp.EvaluateAsync(Session);
166
167 if (Value is SKColor Color)
168 ColorToCss(Result, Color);
169 else if (Value is string s)
170 Result.Append(s);
171 else
172 Result.Append(Expression.ToExpressionString(Value));
173
174 i = k + 1;
175 }
176
177 if (i < c)
178 Result.Append(WebContent[i..]);
179
180 return Result.ToString();
181 }
182 finally
183 {
184 if (PreserveVariables)
185 Session.Pop();
186 }
187 }
188
194 public static string ColorToCss(SKColor Color)
195 {
196 StringBuilder sb = new StringBuilder();
197 ColorToCss(sb, Color);
198 return sb.ToString();
199 }
200
207 public static void ColorToCss(StringBuilder Result, SKColor Color)
208 {
209 if (Color.Alpha == 255)
210 {
211 Result.Append('#');
212 Result.Append(Color.Red.ToString("X2"));
213 Result.Append(Color.Green.ToString("X2"));
214 Result.Append(Color.Blue.ToString("X2"));
215 }
216 else
217 {
218 Result.Append("rgba(");
219 Result.Append(Color.Red.ToString());
220 Result.Append(',');
221 Result.Append(Color.Green.ToString());
222 Result.Append(',');
223 Result.Append(Color.Blue.ToString());
224 Result.Append(',');
225 Result.Append(Expression.ToString(Color.Alpha / 255.0));
226 Result.Append(')');
227 }
228 }
229
230 }
231}
Contains the state of a content conversion process.
Variables Session
Session states.
bool HasError
If an error should be returned.
Stream To
Stream pointing to where binary representation of content is to be sent.
string FromFileName
If the content is coming from a file, this parameter contains the name of that file....
Stream From
Stream pointing to binary representation of content.
Converts CSSX-files to CSS, by evaluating emebedded script and replacing it with results.
Definition: CssxToCss.cs:21
static Task< string > Convert(string Cssx, ConversionState State, string FileName)
Converts CSSX to CSS, using the current theme
Definition: CssxToCss.cs:76
async Task< bool > ConvertAsync(ConversionState State)
Performs the actual conversion.
Definition: CssxToCss.cs:49
string[] ToContentTypes
Converts content to these content types.
Definition: CssxToCss.cs:37
static Task< string > Convert(string Cssx, Variables Session, string FileName)
Converts CSSX to CSS, using the current theme
Definition: CssxToCss.cs:88
CssxToCss()
Converts CSSX-files to CSS, by evaluating emebedded script and replacing it with results.
Definition: CssxToCss.cs:25
string[] FromContentTypes
Converts content from these content types.
Definition: CssxToCss.cs:32
static void ColorToCss(StringBuilder Result, SKColor Color)
Converts a color to its CSS representation.
Definition: CssxToCss.cs:207
Grade ConversionGrade
How well the content is converted.
Definition: CssxToCss.cs:42
static string ColorToCss(SKColor Color)
Converts a color to its CSS representation.
Definition: CssxToCss.cs:194
static ThemeDefinition GetCurrentTheme(SessionVariables Variables)
Gets the current theme definition, based on the host information available in the session varaibles.
Definition: Theme.cs:71
Contains properties for a theme.
void Prepare(Variables Variables)
Prepares a collection of variables for transforming content based on a theme.
Collection of session variables.
Static class managing binary representations of strings.
Definition: Strings.cs:10
static Encoding Utf8WithBom
UTF-8 encoding with Byte Order Mark (BOM)
Definition: Strings.cs:231
Class managing a script expression.
Definition: Expression.cs:41
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4461
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
static string ToExpressionString(object Value)
Converts an object to a string, that can be parsed as part of an expression.
Definition: Expression.cs:5052
Collection of variables.
Definition: Variables.cs:25
virtual void Push()
Pushes the current set of variables to the stack. This state is restored by calling Pop....
Definition: Variables.cs:184
virtual void Pop()
Pops a previously stored set of variables from the stack. Variables are stored on the stack by callin...
Definition: Variables.cs:206
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7