Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ToMarkdown.cs
1using SkiaSharp;
2using System;
3using System.Collections.Generic;
4using System.Text;
5using System.Xml;
7using Waher.Script;
15
17{
22 {
32 {
33 }
34
38 public override string FunctionName => nameof(ToMarkdown);
39
47 {
48 return new StringValue(Evaluate(Argument));
49 }
50
56 public static string Evaluate(IElement Argument)
57 {
58 if (Argument.AssociatedObjectValue is string s)
59 return s;
60
61 if (Argument.AssociatedObjectValue is IToMatrix ToMatrix)
63
64 if (Argument is IMatrix Matrix)
65 return MatrixToMarkdown(Matrix);
66 else if (Argument is IVector Vector)
67 return VectorToMarkdown(Vector);
68 else if (Argument is ISet Set)
69 return SetToMarkdown(Set);
70 else if (Argument is Graph Graph)
71 return GraphToMarkdown(Graph);
72 else if (Argument is PixelInformation Pixels)
73 return PixelsToMarkdown(Pixels);
74 else if (Argument is SKImage Image)
75 return ImageToMarkdown(Image);
76 else
77 return Argument.AssociatedObjectValue?.ToString();
78 }
79
85 public static string MatrixToMarkdown(IMatrix Matrix)
86 {
87 StringBuilder Markdown = new StringBuilder();
88 MatrixToMarkdown(Matrix, Markdown);
89 return Markdown.ToString();
90 }
91
97 public static void MatrixToMarkdown(IMatrix Matrix, StringBuilder Markdown)
98 {
99 int Cols = Matrix.Columns;
100 int Rows = Matrix.Rows;
101 int Col, Row;
102 IElement E;
103 string s;
104
105 if (Matrix is ObjectMatrix OM && OM.HasColumnNames)
106 {
107 foreach (string Name in OM.ColumnNames)
108 {
109 Markdown.Append("| ");
110 Markdown.Append(MarkdownDocument.Encode(Name));
111 Markdown.Append(' ');
112 }
113 }
114 else
115 {
116 for (Col = 1; Col <= Cols; Col++)
117 {
118 Markdown.Append("| ");
119 Markdown.Append(Col.ToString());
120 Markdown.Append(' ');
121 }
122 }
123
124 Markdown.AppendLine("|");
125
126 for (Col = 0; Col < Cols; Col++)
127 {
128 switch (ColumnAlignment(Matrix, Col))
129 {
130 case TextAlignment.Left:
131 Markdown.Append("|:--");
132 break;
133
134 case TextAlignment.Center:
135 Markdown.Append("|:-:");
136 break;
137
138 case TextAlignment.Right:
139 Markdown.Append("|--:");
140 break;
141
142 default:
143 Markdown.Append("|---");
144 break;
145 }
146 }
147
148 Markdown.AppendLine("|");
149
150 LinkedList<KeyValuePair<string, string>> Notes = null;
151
152 for (Row = 0; Row < Rows; Row++)
153 {
154 for (Col = 0; Col < Cols; Col++)
155 {
156 Markdown.Append("| ");
157
158 E = Matrix.GetElement(Col, Row);
159
160 if (!(E?.AssociatedObjectValue is null))
161 {
162 s = Evaluate(E);
163
164 if (s.IndexOfAny(CommonTypes.CRLF) >= 0)
165 {
166 string NoteId = "n" + Guid.NewGuid().ToString().Replace("-", string.Empty);
167
168 if (Notes is null)
169 Notes = new LinkedList<KeyValuePair<string, string>>();
170
171 Notes.AddLast(new KeyValuePair<string, string>(NoteId, s));
172
173 Markdown.Append("[^");
174 Markdown.Append(NoteId);
175 Markdown.Append(']');
176 }
177 else
178 Markdown.Append(s);
179
180 Markdown.Append(' ');
181 }
182 }
183
184 Markdown.AppendLine("|");
185 }
186
187 if (!(Notes is null))
188 {
189 foreach (KeyValuePair<string, string> Note in Notes)
190 {
191 Markdown.AppendLine();
192 Markdown.Append("[^");
193 Markdown.Append(Note.Key);
194 Markdown.Append("]:");
195
196 foreach (string Row2 in Note.Value.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n'))
197 {
198 Markdown.Append('\t');
199 Markdown.AppendLine(Row2);
200 }
201 }
202 }
203 }
204
205 private static TextAlignment ColumnAlignment(IMatrix Matrix, int Column)
206 {
207 int Row;
208 int Rows = Matrix.Rows;
209 TextAlignment? Result = null;
210 TextAlignment Cell;
211 IElement E;
212 object Obj;
213
214 for (Row = 0; Row < Rows; Row++)
215 {
216 E = Matrix.GetElement(Column, Row);
217 Obj = E.AssociatedObjectValue;
218
219 if (Obj is null)
220 continue;
221
222 if (Obj is string)
223 Cell = TextAlignment.Left;
224 else if (Obj is double)
225 Cell = TextAlignment.Right;
226 else if (Obj is bool)
227 Cell = TextAlignment.Center;
228 else
229 {
230 switch (Convert.GetTypeCode(Obj))
231 {
232 case TypeCode.Boolean:
233 Cell = TextAlignment.Center;
234 break;
235
236 case TypeCode.Char:
237 case TypeCode.DateTime:
238 case TypeCode.String:
239 Cell = TextAlignment.Left;
240 break;
241
242 case TypeCode.Byte:
243 case TypeCode.Decimal:
244 case TypeCode.Double:
245 case TypeCode.Int16:
246 case TypeCode.Int32:
247 case TypeCode.Int64:
248 case TypeCode.SByte:
249 case TypeCode.Single:
250 case TypeCode.UInt16:
251 case TypeCode.UInt32:
252 case TypeCode.UInt64:
253 Cell = TextAlignment.Right;
254 break;
255
256 case TypeCode.Empty:
257 case TypeCode.Object:
258 default:
259 continue;
260 }
261 }
262
263 if (!Result.HasValue)
264 Result = Cell;
265 else if (Result.Value != Cell)
266 return TextAlignment.Left;
267 }
268
269 return Result ?? TextAlignment.Left;
270 }
271
277 public static string VectorToMarkdown(IVector Vector)
278 {
279 return EnumerableToMarkdown(Vector.ChildElements);
280 }
281
282 private static string EnumerableToMarkdown(IEnumerable<IElement> Elements)
283 {
284 StringBuilder Markdown = new StringBuilder();
285
286 foreach (IElement E in Elements)
287 {
288 Markdown.AppendLine(E.AssociatedObjectValue?.ToString() ?? string.Empty);
289 Markdown.AppendLine();
290 }
291
292 return Markdown.ToString();
293 }
294
300 public static string SetToMarkdown(ISet Set)
301 {
302 return EnumerableToMarkdown(Set.ChildElements);
303 }
304
310 public static string GraphToMarkdown(Graph Graph)
311 {
312 StringBuilder Markdown = new StringBuilder();
313 GraphToMarkdown(Graph, Markdown);
314 return Markdown.ToString();
315 }
316
322 public static void GraphToMarkdown(Graph Graph, StringBuilder Markdown)
323 {
324 Markdown.AppendLine("```graph");
325 Graph.ToXml(Markdown);
326 Markdown.AppendLine();
327 Markdown.AppendLine("```");
328 Markdown.AppendLine();
329 }
330
335 public static string PixelsToMarkdown(PixelInformation Pixels)
336 {
337 StringBuilder Markdown = new StringBuilder();
338 PixelsToMarkdown(Pixels, Markdown);
339 return Markdown.ToString();
340 }
341
347 public static void PixelsToMarkdown(PixelInformation Pixels, StringBuilder Markdown)
348 {
349 byte[] Bin = Pixels.EncodeAsPng();
350
351 Markdown.AppendLine("```image/png");
352 Markdown.AppendLine(Convert.ToBase64String(Bin));
353 Markdown.AppendLine("```");
354 Markdown.AppendLine();
355 }
356
361 public static string ImageToMarkdown(SKImage Image)
362 {
363 StringBuilder Markdown = new StringBuilder();
364 ImageToMarkdown(Image, Markdown);
365 return Markdown.ToString();
366 }
367
373 public static void ImageToMarkdown(SKImage Image, StringBuilder Markdown)
374 {
375 using (SKData Data = Image.Encode(SKEncodedImageFormat.Png, 100))
376 {
377 byte[] Bin = Data.ToArray();
378
379 Markdown.AppendLine("```image/png");
380 Markdown.AppendLine(Convert.ToBase64String(Bin));
381 Markdown.AppendLine("```");
382 Markdown.AppendLine();
383 }
384
385 }
386 }
387}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static readonly char[] CRLF
Contains the CR LF character sequence.
Definition: CommonTypes.cs:17
Converts an element to a markdown string.
Definition: ToMarkdown.cs:22
override string FunctionName
Name of the function
Definition: ToMarkdown.cs:38
static void PixelsToMarkdown(PixelInformation Pixels, StringBuilder Markdown)
Converts pixels to Markdown.
Definition: ToMarkdown.cs:347
static void ImageToMarkdown(SKImage Image, StringBuilder Markdown)
Converts an image to Markdown.
Definition: ToMarkdown.cs:373
override IElement Evaluate(IElement Argument, Variables Variables)
Evaluates the function.
Definition: ToMarkdown.cs:46
static string ImageToMarkdown(SKImage Image)
Converts an image to Markdown.
Definition: ToMarkdown.cs:361
static string MatrixToMarkdown(IMatrix Matrix)
Converts a matrix to Markdown.
Definition: ToMarkdown.cs:85
static string SetToMarkdown(ISet Set)
Converts a set to Markdown.
Definition: ToMarkdown.cs:300
static string PixelsToMarkdown(PixelInformation Pixels)
Converts pixels to Markdown.
Definition: ToMarkdown.cs:335
static void GraphToMarkdown(Graph Graph, StringBuilder Markdown)
Converts a graph to Markdown.
Definition: ToMarkdown.cs:322
static string Evaluate(IElement Argument)
Converts an element to a markdown string.
Definition: ToMarkdown.cs:56
static string GraphToMarkdown(Graph Graph)
Converts a graph to Markdown.
Definition: ToMarkdown.cs:310
static string VectorToMarkdown(IVector Vector)
Converts a vector to Markdown.
Definition: ToMarkdown.cs:277
ToMarkdown(ScriptNode Argument, int Start, int Length, Expression Expression)
Converts an element to a markdown string.
Definition: ToMarkdown.cs:30
static void MatrixToMarkdown(IMatrix Matrix, StringBuilder Markdown)
Converts a matrix to Markdown.
Definition: ToMarkdown.cs:97
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
Base class for all types of sets.
Definition: Set.cs:14
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Set.cs:73
Class managing a script expression.
Definition: Expression.cs:39
Base class for graphs.
Definition: Graph.cs:79
string ToXml()
Exports the graph to XML.
Definition: Graph.cs:1499
Contains pixel information
virtual byte[] EncodeAsPng()
Encodes the pixels into a binary PNG image.
Base class for funcions of one variable.
ScriptNode Argument
Function argument.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
bool HasColumnNames
If the matrix has column names defined.
ToMatrix(ScriptNode Operand, bool NullCheck, int Start, int Length, Expression Expression)
To-Matrix operator.
Definition: ToMatrix.cs:22
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:49
Basic interface for matrices.
Definition: IMatrix.cs:9
IElement GetElement(int Column, int Row)
Gets an element of the matrix.
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
Interface for objects that can be converted into matrices.
Definition: IToMatrix.cs:9
TextAlignment
Text alignment of contents.