Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FromMarkdown.cs
1using System;
2using System.Globalization;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
10using Waher.Script;
17
19{
24 {
34 {
35 }
36
40 public override string FunctionName => nameof(FromMarkdown);
41
46 public override bool IsAsynchronous => true;
47
55 {
56 return this.EvaluateScalarAsync(Argument, Variables).Result;
57 }
58
65 public override Task<IElement> EvaluateScalarAsync(string Argument, Variables Variables)
66 {
67 return Evaluate(Argument, this);
68 }
69
76 public static async Task<IElement> Evaluate(string Argument, ScriptNode Node)
77 {
79 IElement Result = null;
80 ChunkedList<IElement> Results = null;
81 IElement Item = null;
83 int i, c;
84
85 while (!(Loop is null))
86 {
87 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
88 {
89 Item = await Evaluate(Loop[i]);
90
91 if (Result is null)
92 Result = Item;
93 else
94 {
95 if (Results is null)
96 {
97 Results = new ChunkedList<IElement>
98 {
99 Result
100 };
101 }
102
103 Results.Add(Item);
104 }
105 }
106
107 Loop = Loop.Next;
108 }
109
110 if (Results is null)
111 return Item ?? ObjectValue.Null;
112 else
113 return VectorDefinition.Encapsulate(Results, false, Node);
114 }
115
121 public static async Task<IElement> Evaluate(MarkdownElement Element)
122 {
123 if (Element is Model.BlockElements.Table Table)
124 {
125 if (Table.Headers.Length == 1)
126 {
127 MarkdownElement[] Headers = Table.Headers[0];
128 int i, Columns = Headers.Length;
129 string[] Headers2 = new string[Columns];
130 int Rows = Table.Rows.Length;
133 int j, d;
134
135 for (i = 0; i < Columns; i++)
136 Headers2[i] = ToString(await Evaluate(Headers[i])) ?? string.Empty;
137
138 foreach (MarkdownElement[] Row in Table.Rows)
139 {
140 for (j = 0, d = Row.Length; j < d; j++)
141 {
142 E = Row[j];
143
144 if (E is null)
145 Elements.Add(ObjectValue.Null);
146 else
147 Elements.Add(await Evaluate(E));
148 }
149 }
150
151 ObjectMatrix M = new ObjectMatrix(Rows, Columns, Elements)
152 {
153 ColumnNames = Headers2
154 };
155
156 return M;
157 }
158 }
159 else if (Element is Model.BlockElements.CodeBlock CodeBlock)
160 {
161 string Language = CodeBlock.Language;
162 int i = Language.IndexOf(':');
163
164 if (i > 0)
165 Language = Language.Substring(0, i);
166
167 switch (Language.ToLower())
168 {
169 case "graph":
170 return await Model.CodeContent.GraphContent.GetGraph(CodeBlock.Rows);
171
172 case "xml":
173 StringBuilder sb = new StringBuilder();
174
175 foreach (string Row in CodeBlock.Rows)
176 sb.AppendLine(Row);
177
178 XmlDocument Doc = XML.ParseXml(sb.ToString());
179
180 return new ObjectValue(Doc);
181
182 default:
183 ICodeContentHtmlRenderer Renderer = CodeBlock.CodeContentHandler<ICodeContentHtmlRenderer>();
184
185 if (Renderer is IImageCodeContent ImageCodeContent)
186 {
187 PixelInformation Pixels = await ImageCodeContent.GenerateImage(CodeBlock.Rows, Language, Element.Document);
188 return new GraphBitmap(Element.Document.Settings.Variables ?? new Variables(), Pixels);
189 }
190 break;
191 }
192 }
193 else if (Element is null)
194 return ObjectValue.Null;
195
196 using (TextRenderer Renderer2 = new TextRenderer())
197 {
198 await Element.Render(Renderer2);
199 string s = Renderer2.ToString().Trim();
200
201 if (double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out double d))
202 return new DoubleNumber(d);
203 else if (CommonTypes.TryParse(s, out bool b))
204 return new BooleanValue(b);
205 else if (Measurement.TryParse(s, out Measurement M))
206 return M;
207 else if (PhysicalQuantity.TryParse(s, out PhysicalQuantity Q))
208 return Q;
209 else if (XML.TryParse(s, out DateTime TP))
210 return new DateTimeValue(TP);
211 else if (Element is Model.SpanElements.InlineText)
212 return new StringValue(s);
213 else
214 return new ObjectValue(Element);
215 }
216 }
217 }
218}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
Converts markdown to an element.
Definition: FromMarkdown.cs:24
override string FunctionName
Name of the function
Definition: FromMarkdown.cs:40
FromMarkdown(ScriptNode Argument, int Start, int Length, Expression Expression)
Converts markdown to an element.
Definition: FromMarkdown.cs:32
override IElement EvaluateScalar(string Argument, Variables Variables)
Evaluates the function.
Definition: FromMarkdown.cs:54
static async Task< IElement > Evaluate(MarkdownElement Element)
Converts a Markdown element to a script element.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: FromMarkdown.cs:46
static async Task< IElement > Evaluate(string Argument, ScriptNode Node)
Converts a Markdown string to a script element.
Definition: FromMarkdown.cs:76
override Task< IElement > EvaluateScalarAsync(string Argument, Variables Variables)
Evaluates the function.
Definition: FromMarkdown.cs:65
Contains a markdown document. This markdown document class supports original markdown,...
static Task< MarkdownDocument > CreateAsync(string MarkdownText, params Type[] TransparentExceptionTypes)
Contains a markdown document. This markdown document class supports original markdown,...
ChunkedList< MarkdownElement > Elements
Markdown elements making up the document.
Abstract base class for all markdown elements.
Abstract base class for Markdown renderers.
Definition: Renderer.cs:15
override string ToString()
Returns the renderer output.
Definition: Renderer.cs:130
Renders plain text from a Markdown document.
Definition: TextRenderer.cs:18
Helps with common XML-related tasks.
Definition: XML.cs:21
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:892
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
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 elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:13
Contains pixel information
Base class for funcions of one scalar string 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
Boolean-valued number.
Definition: BooleanValue.cs:12
static bool TryParse(string s, out Measurement Value)
Tries to parse a string to a physical quantity.
Definition: Measurement.cs:388
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
static bool TryParse(string s, out PhysicalQuantity Value)
Tries to parse a string to a physical quantity.
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Collection of variables.
Definition: Variables.cs:25
Interface for all markdown handlers of code content that generates an image output.
Basic interface for all types of elements.
Definition: IElement.cs:21