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.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
9using Waher.Script;
16
18{
23 {
33 {
34 }
35
39 public override string FunctionName => nameof(FromMarkdown);
40
45 public override bool IsAsynchronous => true;
46
54 {
55 return this.EvaluateScalarAsync(Argument, Variables).Result;
56 }
57
64 public override Task<IElement> EvaluateScalarAsync(string Argument, Variables Variables)
65 {
66 return Evaluate(Argument, this);
67 }
68
75 public static async Task<IElement> Evaluate(string Argument, ScriptNode Node)
76 {
78 IElement Result = null;
79 LinkedList<IElement> Results = null;
80 IElement Item = null;
81
82 foreach (MarkdownElement E in Doc.Elements)
83 {
84 Item = await Evaluate(E);
85
86 if (Result is null)
87 Result = Item;
88 else
89 {
90 if (Results is null)
91 {
92 Results = new LinkedList<IElement>();
93 Results.AddLast(Result);
94 }
95
96 Results.AddLast(Item);
97 }
98 }
99
100 if (Results is null)
101 return Item ?? ObjectValue.Null;
102 else
103 return VectorDefinition.Encapsulate(Results, false, Node);
104 }
105
111 public static async Task<IElement> Evaluate(MarkdownElement Element)
112 {
113 if (Element is Model.BlockElements.Table Table)
114 {
115 if (Table.Headers.Length == 1)
116 {
117 MarkdownElement[] Headers = Table.Headers[0];
118 int i, Columns = Headers.Length;
119 string[] Headers2 = new string[Columns];
120 int Rows = Table.Rows.Length;
121 LinkedList<IElement> Elements = new LinkedList<IElement>();
122
123 for (i = 0; i < Columns; i++)
124 Headers2[i] = (await Evaluate(Headers[i])).AssociatedObjectValue?.ToString() ?? string.Empty;
125
126 foreach (MarkdownElement[] Row in Table.Rows)
127 {
128 foreach (MarkdownElement E in Row)
129 {
130 if (E is null)
131 Elements.AddLast(ObjectValue.Null);
132 else
133 Elements.AddLast(await Evaluate(E));
134 }
135 }
136
137 ObjectMatrix M = new ObjectMatrix(Rows, Columns, Elements)
138 {
139 ColumnNames = Headers2
140 };
141
142 return M;
143 }
144 }
145 else if (Element is Model.BlockElements.CodeBlock CodeBlock)
146 {
147 string Language = CodeBlock.Language;
148 int i = Language.IndexOf(':');
149
150 if (i > 0)
151 Language = Language.Substring(0, i);
152
153 switch (Language.ToLower())
154 {
155 case "graph":
156 return await Model.CodeContent.GraphContent.GetGraph(CodeBlock.Rows);
157
158 case "xml":
159 StringBuilder sb = new StringBuilder();
160
161 foreach (string Row in CodeBlock.Rows)
162 sb.AppendLine(Row);
163
164 XmlDocument Doc = new XmlDocument();
165 Doc.LoadXml(sb.ToString());
166
167 return new ObjectValue(Doc);
168
169 default:
170 ICodeContentHtmlRenderer Renderer = CodeBlock.CodeContentHandler<ICodeContentHtmlRenderer>();
171
172 if (Renderer is IImageCodeContent ImageCodeContent)
173 {
174 PixelInformation Pixels = await ImageCodeContent.GenerateImage(CodeBlock.Rows, Language, Element.Document);
175 return new GraphBitmap(Element.Document.Settings.Variables, Pixels);
176 }
177 break;
178 }
179 }
180 else if (Element is null)
181 return ObjectValue.Null;
182
183 using (TextRenderer Renderer2 = new TextRenderer())
184 {
185 await Element.Render(Renderer2);
186 string s = Renderer2.ToString().Trim();
187
188 if (CommonTypes.TryParse(s, out double d))
189 return new DoubleNumber(d);
190 else if (CommonTypes.TryParse(s, out bool b))
191 return new BooleanValue(b);
192 else if (Measurement.TryParse(s, out Measurement M))
193 return M;
194 else if (PhysicalQuantity.TryParse(s, out PhysicalQuantity Q))
195 return Q;
196 else if (XML.TryParse(s, out DateTime TP))
197 return new DateTimeValue(TP);
198 else if (Element is Model.SpanElements.InlineText)
199 return new StringValue(s);
200 else
201 return new ObjectValue(Element);
202 }
203 }
204 }
205}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Converts markdown to an element.
Definition: FromMarkdown.cs:23
override string FunctionName
Name of the function
Definition: FromMarkdown.cs:39
FromMarkdown(ScriptNode Argument, int Start, int Length, Expression Expression)
Converts markdown to an element.
Definition: FromMarkdown.cs:31
override IElement EvaluateScalar(string Argument, Variables Variables)
Evaluates the function.
Definition: FromMarkdown.cs:53
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:45
static async Task< IElement > Evaluate(string Argument, ScriptNode Node)
Converts a Markdown string to a script element.
Definition: FromMarkdown.cs:75
override Task< IElement > EvaluateScalarAsync(string Argument, Variables Variables)
Evaluates the function.
Definition: FromMarkdown.cs:64
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,...
Abstract base class for all markdown elements.
Abstract base class for Markdown renderers.
Definition: Renderer.cs:14
override string ToString()
Returns the renderer output.
Definition: Renderer.cs:130
Renders plain text from a Markdown document.
Definition: TextRenderer.cs:16
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
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
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:86
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:20