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 System;
2using System.IO;
3using System.Text;
4using System.Threading.Tasks;
5using SkiaSharp;
6using Waher.Content;
11using Waher.Script;
12
14{
19 {
23 public CssxToCss()
24 {
25 }
26
30 public string[] FromContentTypes => new string[] { "text/x-cssx" };
31
35 public string[] ToContentTypes => new string[] { CssCodec.ContentType };
36
40 public Grade ConversionGrade => Grade.Perfect;
41
47 public async Task<bool> ConvertAsync(ConversionState State)
48 {
49 string Cssx;
50
51 using (StreamReader rd = new StreamReader(State.From))
52 {
53 Cssx = await rd.ReadToEndAsync();
54 }
55
56 string Css = await Convert(Cssx, State.Session, State.FromFileName);
57
58 byte[] Data = Utf8WithBOM.GetBytes(Css);
59 await State.To.WriteAsync(Data, 0, Data.Length);
60 State.ToContentType += "; charset=utf-8";
61
62 return false;
63 }
64
72 public static Task<string> Convert(string Cssx, Variables Session, string FileName)
73 {
74 return Convert(Cssx, Session, FileName, true);
75 }
76
86 internal static async Task<string> Convert(string Cssx, Variables Session, string FileName, bool LockSession)
87 {
88 bool Pushed = false;
89
90 if (Session is null)
91 throw new ArgumentNullException("No session defined.", nameof(Session));
92
93 if (LockSession)
94 await Session.LockAsync();
95 try
96 {
97 if (LockSession)
98 {
99 Session.Push();
100 Pushed = true;
101 }
102
104 Def?.Prepare(Session);
105
106 StringBuilder Result = new StringBuilder();
107 Expression Exp;
108 int i = 0;
109 int c = Cssx.Length;
110 int j, k;
111 string Script;
112 object Value;
113
114 while (i < c)
115 {
116 j = Cssx.IndexOf('¤', i);
117 if (j < 0)
118 break;
119
120 if (j > i)
121 Result.Append(Cssx.Substring(i, j - i));
122
123 k = Cssx.IndexOf('¤', j + 1);
124 if (k < 0)
125 break;
126
127 Script = Cssx.Substring(j + 1, k - j - 1);
128 Exp = new Expression(Script, FileName);
129 Value = await Exp.EvaluateAsync(Session);
130
131 if (Value is SKColor Color)
132 {
133 if (Color.Alpha == 255)
134 {
135 Result.Append('#');
136 Result.Append(Color.Red.ToString("X2"));
137 Result.Append(Color.Green.ToString("X2"));
138 Result.Append(Color.Blue.ToString("X2"));
139 }
140 else
141 {
142 Result.Append("rgba(");
143 Result.Append(Color.Red.ToString());
144 Result.Append(',');
145 Result.Append(Color.Green.ToString());
146 Result.Append(',');
147 Result.Append(Color.Blue.ToString());
148 Result.Append(',');
149 Result.Append(Expression.ToString(Color.Alpha / 255.0));
150 Result.Append(')');
151 }
152 }
153 else if (Value is string s)
154 Result.Append(s);
155 else
156 Result.Append(Expression.ToString(Value));
157
158 i = k + 1;
159 }
160
161 if (i < c)
162 Result.Append(Cssx.Substring(i));
163
164 return Result.ToString();
165 }
166 finally
167 {
168 if (LockSession)
169 {
170 if (Pushed)
171 Session.Pop();
172
173 Session.Release();
174 }
175 }
176 }
177
178 internal static readonly System.Text.Encoding Utf8WithBOM = new UTF8Encoding(true);
179
180 }
181}
Contains the state of a content conversion process.
Variables Session
Session states.
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:19
async Task< bool > ConvertAsync(ConversionState State)
Performs the actual conversion.
Definition: CssxToCss.cs:47
string[] ToContentTypes
Converts content to these content types.
Definition: CssxToCss.cs:35
static Task< string > Convert(string Cssx, Variables Session, string FileName)
Converts CSSX to CSS, using the current theme
Definition: CssxToCss.cs:72
CssxToCss()
Converts CSSX-files to CSS, by evaluating emebedded script and replacing it with results.
Definition: CssxToCss.cs:23
string[] FromContentTypes
Converts content from these content types.
Definition: CssxToCss.cs:30
Grade ConversionGrade
How well the content is converted.
Definition: CssxToCss.cs:40
static ThemeDefinition GetCurrentTheme(Variables Variables)
Gets the current theme definition, based on the host information available in the session varaibles.
Definition: Theme.cs:69
Contains properties for a theme.
void Prepare(Variables Variables)
Prepares a collection of variables for transforming content based on a theme.
Class managing a script expression.
Definition: Expression.cs:39
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4275
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
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:180
virtual void Pop()
Pops a previously stored set of variables from the stack. Variables are stored on the stack by callin...
Definition: Variables.cs:202
Task LockAsync()
Locks the collection. The collection is by default thread safe. But if longer transactions require un...
Definition: Variables.cs:265
void Release()
Releases the collection, previously locked through a call to Lock().
Definition: Variables.cs:308
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7