forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentInformation.cs
More file actions
234 lines (205 loc) · 5.48 KB
/
DocumentInformation.cs
File metadata and controls
234 lines (205 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Text;
using System.Xml;
using Waher.Content.Markdown.Model;
using Waher.Content.Markdown.Model.BlockElements;
using Waher.Content.Xml;
using Waher.Runtime.Inventory;
using Waher.Script.Graphs;
namespace Waher.Content.Markdown.Consolidation
{
/// <summary>
/// Type of markdown document.
/// </summary>
[Flags]
public enum DocumentType
{
/// <summary>
/// Empty document
/// </summary>
Empty = 1,
/// <summary>
/// Contains a single line containing a number.
/// </summary>
SingleNumber = 3,
/// <summary>
/// Contains a single line of text.
/// </summary>
SingleLine = 7,
/// <summary>
/// Contains a single paragraph of text.
/// </summary>
SingleParagraph = 15,
/// <summary>
/// Contains one code section.
/// </summary>
SingleCode = 17,
/// <summary>
/// Contains one table.
/// </summary>
SingleTable = 33,
/// <summary>
/// Contains one graph.
/// </summary>
SingleGraph = 65,
/// <summary>
/// Contains one block of XML
/// </summary>
SingleXml = 129,
/// <summary>
/// Contains complex content.
/// </summary>
Complex = 511
}
/// <summary>
/// Information about a document.
/// </summary>
public class DocumentInformation
{
private readonly MarkdownDocument markdown;
private readonly DocumentType type;
private readonly string[] rows;
private readonly Type graphType;
private readonly Graph graph;
private readonly Table table;
/// <summary>
/// Information about a document.
/// </summary>
public DocumentInformation(MarkdownDocument Markdown)
{
MarkdownElement First = null;
int i = 0;
bool IsTable = false;
bool IsCode = false;
foreach (MarkdownElement E in Markdown.Elements)
{
if (First is null)
First = E;
i++;
IsTable |= E is Table;
IsCode |= E is CodeBlock;
}
this.markdown = Markdown;
string s = Markdown.MarkdownText.Trim().Replace("\r\n", "\n").Replace('\r', '\n');
this.rows = s.Split('\n');
if (i == 0)
this.type = DocumentType.Empty;
else if (i == 1)
{
int c = this.rows.Length;
if (IsTable)
{
this.table = (Table)First;
this.type = DocumentType.SingleTable;
}
else if (IsCode)
{
if (c >= 3 &&
this.rows[0].StartsWith("```", StringComparison.CurrentCultureIgnoreCase) &&
this.rows[1].StartsWith("<", StringComparison.CurrentCultureIgnoreCase) &&
this.rows[c - 2].EndsWith(">", StringComparison.CurrentCultureIgnoreCase) &&
this.rows[c - 1].EndsWith("```"))
{
if (c >= 4 &&
this.rows[0].StartsWith("```Graph", StringComparison.CurrentCultureIgnoreCase) &&
this.rows[1].StartsWith("<Graph", StringComparison.CurrentCultureIgnoreCase) &&
this.rows[c - 2].EndsWith("</Graph>", StringComparison.CurrentCultureIgnoreCase) &&
this.rows[c - 1].EndsWith("```"))
{
try
{
StringBuilder sb = new StringBuilder();
for (i = 1, c--; i < c; i++)
sb.AppendLine(Rows[i]);
XmlDocument Xml = new XmlDocument();
Xml.LoadXml(sb.ToString());
if (!(Xml.DocumentElement is null) &&
Xml.DocumentElement.LocalName == Graph.GraphLocalName &&
Xml.DocumentElement.NamespaceURI == Graph.GraphNamespace)
{
string TypeName = XML.Attribute(Xml.DocumentElement, "type");
this.graphType = Types.GetType(TypeName);
if (this.graphType is null)
this.type = DocumentType.SingleXml;
else
{
this.graph = (Graph)Activator.CreateInstance(this.graphType);
this.graph.SameScale = XML.Attribute(Xml.DocumentElement, "sameScale", false);
foreach (XmlNode N in Xml.DocumentElement.ChildNodes)
{
if (N is XmlElement E)
{
this.graph.ImportGraph(E);
break;
}
}
this.type = DocumentType.SingleGraph;
}
}
else
this.type = DocumentType.SingleXml;
}
catch (Exception)
{
this.type = DocumentType.SingleXml;
}
}
else
this.type = DocumentType.SingleXml;
}
else
this.type = DocumentType.SingleCode;
}
else
{
if (string.IsNullOrEmpty(s))
this.type = DocumentType.Empty;
else
{
if (c > 1)
this.type = DocumentType.SingleParagraph;
else if (IsNumeric(this.rows[0]))
this.type = DocumentType.SingleNumber;
else
this.type = DocumentType.SingleLine;
}
}
}
else
this.type = DocumentType.Complex;
}
private static bool IsNumeric(string s)
{
if (s.StartsWith("<") && s.EndsWith(">"))
{
int i = s.IndexOf('>');
int j = s.LastIndexOf('<');
return (i < j && IsNumeric(s.Substring(i + 1, j - i - 1)));
}
else if (s.Length > 2 && s.StartsWith("`") && s.EndsWith("`"))
return IsNumeric(s.Substring(1, s.Length - 2));
else
return CommonTypes.TryParse(s, out double _);
}
/// <summary>
/// Markdown document.
/// </summary>
public MarkdownDocument Markdown => this.markdown;
/// <summary>
/// Document type.
/// </summary>
public DocumentType Type => this.type;
/// <summary>
/// Graph object, if <see cref="DocumentType.SingleGraph"/>
/// </summary>
public Graph Graph => this.graph;
/// <summary>
/// Table object, if <see cref="DocumentType.SingleTable"/>
/// </summary>
public Table Table => this.table;
/// <summary>
/// Rows
/// </summary>
public string[] Rows => this.rows;
}
}