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;
4using System.Text;
7using Waher.Script;
16
18{
23 {
33 {
34 }
35
39 public override string FunctionName => nameof(ToMarkdown);
40
48 {
49 return new StringValue(Evaluate(Argument));
50 }
51
57 public static string Evaluate(IElement Argument)
58 {
59 if (Argument.AssociatedObjectValue is string s)
60 return s;
61
62 if (Argument.AssociatedObjectValue is IToMatrix ToMatrix)
64
65 if (Argument is IMatrix Matrix)
66 return MatrixToMarkdown(Matrix);
67 else if (Argument is IVector Vector)
68 return VectorToMarkdown(Vector);
69 else if (Argument is ISet Set)
70 return SetToMarkdown(Set);
71 else if (Argument is Graph Graph)
72 return GraphToMarkdown(Graph);
73 else if (Argument is PixelInformation Pixels)
74 return PixelsToMarkdown(Pixels);
75 else if (Argument is SKImage Image)
76 return ImageToMarkdown(Image);
77 else
78 return ToString(Argument) ?? string.Empty;
79 }
80
86 public static string MatrixToMarkdown(IMatrix Matrix)
87 {
88 StringBuilder Markdown = new StringBuilder();
89 MatrixToMarkdown(Matrix, Markdown);
90 return Markdown.ToString();
91 }
92
98 public static void MatrixToMarkdown(IMatrix Matrix, StringBuilder Markdown)
99 {
100 int Cols = Matrix.Columns;
101 int Rows = Matrix.Rows;
102 int Col, Row;
103 IElement E;
104 string s;
105
106 if (Matrix is ObjectMatrix OM && OM.HasColumnNames)
107 {
108 foreach (string Name in OM.ColumnNames)
109 {
110 Markdown.Append("| ");
111 Markdown.Append(MarkdownDocument.Encode(Name));
112 Markdown.Append(' ');
113 }
114 }
115 else
116 {
117 for (Col = 1; Col <= Cols; Col++)
118 {
119 Markdown.Append("| ");
120 Markdown.Append(Col.ToString());
121 Markdown.Append(' ');
122 }
123 }
124
125 Markdown.AppendLine("|");
126
127 for (Col = 0; Col < Cols; Col++)
128 {
129 switch (ColumnAlignment(Matrix, Col))
130 {
131 case TextAlignment.Left:
132 Markdown.Append("|:--");
133 break;
134
135 case TextAlignment.Center:
136 Markdown.Append("|:-:");
137 break;
138
139 case TextAlignment.Right:
140 Markdown.Append("|--:");
141 break;
142
143 default:
144 Markdown.Append("|---");
145 break;
146 }
147 }
148
149 Markdown.AppendLine("|");
150
152
153 for (Row = 0; Row < Rows; Row++)
154 {
155 for (Col = 0; Col < Cols; Col++)
156 {
157 Markdown.Append("| ");
158
159 E = Matrix.GetElement(Col, Row);
160
161 if (!(E?.AssociatedObjectValue is null))
162 {
163 s = Evaluate(E);
164
165 if (s.IndexOfAny(CommonTypes.CRLF) >= 0)
166 {
167 string NoteId = "n" + Guid.NewGuid().ToString().Replace("-", string.Empty);
168
169 if (Notes is null)
171
172 Notes.Add(new KeyValuePair<string, string>(NoteId, s));
173
174 Markdown.Append("[^");
175 Markdown.Append(NoteId);
176 Markdown.Append(']');
177 }
178 else
179 Markdown.Append(s);
180
181 Markdown.Append(' ');
182 }
183 }
184
185 Markdown.AppendLine("|");
186 }
187
189 int i, c;
190
191 while (!(Loop is null))
192 {
193 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
194 {
195 KeyValuePair<string, string> Note = Loop[i];
196
197 Markdown.AppendLine();
198 Markdown.Append("[^");
199 Markdown.Append(Note.Key);
200 Markdown.Append("]:");
201
202 foreach (string Row2 in Note.Value.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n'))
203 {
204 Markdown.Append('\t');
205 Markdown.AppendLine(Row2);
206 }
207 }
208
209 Loop = Loop.Next;
210 }
211 }
212
213 private static TextAlignment ColumnAlignment(IMatrix Matrix, int Column)
214 {
215 int Row;
216 int Rows = Matrix.Rows;
217 TextAlignment? Result = null;
218 TextAlignment Cell;
219 IElement E;
220 object Obj;
221
222 for (Row = 0; Row < Rows; Row++)
223 {
224 E = Matrix.GetElement(Column, Row);
225 Obj = E.AssociatedObjectValue;
226
227 if (Obj is null)
228 continue;
229
230 if (Obj is string)
231 Cell = TextAlignment.Left;
232 else if (Obj is double)
233 Cell = TextAlignment.Right;
234 else if (Obj is bool)
235 Cell = TextAlignment.Center;
236 else
237 {
238 switch (Convert.GetTypeCode(Obj))
239 {
240 case TypeCode.Boolean:
241 Cell = TextAlignment.Center;
242 break;
243
244 case TypeCode.Char:
245 case TypeCode.DateTime:
246 case TypeCode.String:
247 Cell = TextAlignment.Left;
248 break;
249
250 case TypeCode.Byte:
251 case TypeCode.Decimal:
252 case TypeCode.Double:
253 case TypeCode.Int16:
254 case TypeCode.Int32:
255 case TypeCode.Int64:
256 case TypeCode.SByte:
257 case TypeCode.Single:
258 case TypeCode.UInt16:
259 case TypeCode.UInt32:
260 case TypeCode.UInt64:
261 Cell = TextAlignment.Right;
262 break;
263
264 case TypeCode.Empty:
265 case TypeCode.Object:
266 default:
267 continue;
268 }
269 }
270
271 if (!Result.HasValue)
272 Result = Cell;
273 else if (Result.Value != Cell)
274 return TextAlignment.Left;
275 }
276
277 return Result ?? TextAlignment.Left;
278 }
279
285 public static string VectorToMarkdown(IVector Vector)
286 {
287 return EnumerableToMarkdown(Vector.ChildElements);
288 }
289
290 private static string EnumerableToMarkdown(IEnumerable<IElement> Elements)
291 {
292 StringBuilder Markdown = new StringBuilder();
293
294 foreach (IElement E in Elements)
295 {
296 Markdown.AppendLine(ToString(E) ?? string.Empty);
297 Markdown.AppendLine();
298 }
299
300 return Markdown.ToString();
301 }
302
308 public static string SetToMarkdown(ISet Set)
309 {
310 return EnumerableToMarkdown(Set.ChildElements);
311 }
312
318 public static string GraphToMarkdown(Graph Graph)
319 {
320 StringBuilder Markdown = new StringBuilder();
321 GraphToMarkdown(Graph, Markdown);
322 return Markdown.ToString();
323 }
324
330 public static void GraphToMarkdown(Graph Graph, StringBuilder Markdown)
331 {
332 Markdown.AppendLine("```graph");
333 Graph.ToXml(Markdown);
334 Markdown.AppendLine();
335 Markdown.AppendLine("```");
336 Markdown.AppendLine();
337 }
338
343 public static string PixelsToMarkdown(PixelInformation Pixels)
344 {
345 StringBuilder Markdown = new StringBuilder();
346 PixelsToMarkdown(Pixels, Markdown);
347 return Markdown.ToString();
348 }
349
355 public static void PixelsToMarkdown(PixelInformation Pixels, StringBuilder Markdown)
356 {
357 byte[] Bin = Pixels.EncodeAsPng();
358
359 Markdown.AppendLine("```image/png");
360 Markdown.AppendLine(Convert.ToBase64String(Bin));
361 Markdown.AppendLine("```");
362 Markdown.AppendLine();
363 }
364
369 public static string ImageToMarkdown(SKImage Image)
370 {
371 StringBuilder Markdown = new StringBuilder();
372 ImageToMarkdown(Image, Markdown);
373 return Markdown.ToString();
374 }
375
381 public static void ImageToMarkdown(SKImage Image, StringBuilder Markdown)
382 {
383 using (SKData Data = Image.Encode(SKEncodedImageFormat.Png, 100))
384 {
385 byte[] Bin = Data.ToArray();
386
387 Markdown.AppendLine("```image/png");
388 Markdown.AppendLine(Convert.ToBase64String(Bin));
389 Markdown.AppendLine("```");
390 Markdown.AppendLine();
391 }
392
393 }
394 }
395}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static readonly char[] CRLF
Contains the CR LF character sequence.
Definition: CommonTypes.cs:19
Converts an element to a markdown string.
Definition: ToMarkdown.cs:23
override string FunctionName
Name of the function
Definition: ToMarkdown.cs:39
static void PixelsToMarkdown(PixelInformation Pixels, StringBuilder Markdown)
Converts pixels to Markdown.
Definition: ToMarkdown.cs:355
static void ImageToMarkdown(SKImage Image, StringBuilder Markdown)
Converts an image to Markdown.
Definition: ToMarkdown.cs:381
override IElement Evaluate(IElement Argument, Variables Variables)
Evaluates the function.
Definition: ToMarkdown.cs:47
static string ImageToMarkdown(SKImage Image)
Converts an image to Markdown.
Definition: ToMarkdown.cs:369
static string MatrixToMarkdown(IMatrix Matrix)
Converts a matrix to Markdown.
Definition: ToMarkdown.cs:86
static string SetToMarkdown(ISet Set)
Converts a set to Markdown.
Definition: ToMarkdown.cs:308
static string PixelsToMarkdown(PixelInformation Pixels)
Converts pixels to Markdown.
Definition: ToMarkdown.cs:343
static void GraphToMarkdown(Graph Graph, StringBuilder Markdown)
Converts a graph to Markdown.
Definition: ToMarkdown.cs:330
static string Evaluate(IElement Argument)
Converts an element to a markdown string.
Definition: ToMarkdown.cs:57
static string GraphToMarkdown(Graph Graph)
Converts a graph to Markdown.
Definition: ToMarkdown.cs:318
static string VectorToMarkdown(IVector Vector)
Converts a vector to Markdown.
Definition: ToMarkdown.cs:285
ToMarkdown(ScriptNode Argument, int Start, int Length, Expression Expression)
Converts an element to a markdown string.
Definition: ToMarkdown.cs:31
static void MatrixToMarkdown(IMatrix Matrix, StringBuilder Markdown)
Converts a matrix to Markdown.
Definition: ToMarkdown.cs:98
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 ...
Node referencing a chunk in a ChunkedList<T>
Definition: ChunkNode.cs:11
ChunkNode< T > Next
Next chunk
Definition: ChunkNode.cs:26
int Pos
Index after the last element in chunk.
Definition: ChunkNode.cs:51
int Start
Index of first element in chunk.
Definition: ChunkNode.cs:46
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
ChunkNode< T > FirstChunk
First chunk
Definition: ChunkedList.cs:259
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
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:84
Class managing a script expression.
Definition: Expression.cs:41
Base class for graphs.
Definition: Graph.cs:88
string ToXml()
Exports the graph to XML.
Definition: Graph.cs:1515
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:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:50
Basic interface for matrices.
Definition: IMatrix.cs:7
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.