This repository was archived by the owner on Jan 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReCollectTextCommon.cs
More file actions
306 lines (277 loc) · 9.07 KB
/
Copy pathReCollectTextCommon.cs
File metadata and controls
306 lines (277 loc) · 9.07 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections.Generic;
using System.Net;
using HtmlAgilityPack;
#if __ANDROID__
using Android.Graphics;
#endif
namespace ReCollect
{
public partial class ReText
{
public int FontSize = 18;
public int ImageSize = 100;
#if __ANDROID__
public Typeface Font = null;
#endif
#if __IOS__
public string FontName = "HelveticaNeue";
public string ItalicFontName = "HelveticaNeue-Italic";
public string BoldFontName = "HelveticaNeue-Bold";
public string BoldItalicFontName = "HelveticaNeue-BoldItalic";
#endif
string Html;
public ReColor LinkColor = ReColor.Blue; // Default link colour
public ReColor TextColor = ReColor.Black; // Default text colour
private bool isOrderedList = false;
int count = 1;
public ReText(string html)
{
// Decode any HTML entities
Html = System.Web.HttpUtility.HtmlDecode(html.Replace("\n", ""));
}
TextWithStyles ParseHtml()
{
// Parse HTML
var doc = new HtmlDocument();
doc.LoadHtml(Html);
// Emit Text with Attributes
return EmitTextWithAttrs(doc.DocumentNode, FontSize);
}
class TextWithStyles
{
public string Text = "";
public Stack<StyleWithRange> Styles = new Stack<StyleWithRange> { };
}
class StyleWithRange
{
public int Offset;
public int Length;
public TextStyle Style;
public void Shift(int offset)
{
Offset += offset;
}
}
bool HasParent(HtmlNode node, string name1, string name2 = "")
{
foreach (var ancestor in node.AncestorsAndSelf())
{
if (ancestor.Name == name1 || ancestor.Name == name2)
return true;
}
return false;
}
TextWithStyles EmitTextWithAttrs(HtmlNode node, int fontSize)
{
var node_text = new TextWithStyles();
TextStyle node_style = null;
/**
* Block level nodes:
*/
switch (node.Name)
{
case "p":
case "div":
case "br":
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
// Switch <p></p> into strings with newlines UNLESS this is the last <p> in the document
if (node.ParentNode.NodeType != HtmlNodeType.Document || node.NextSibling != null)
{
node.InnerHtml = node.InnerHtml + "\n";
}
break;
case "img":
node.InnerHtml = node.InnerHtml + " img ";
break;
case "li":
if (isOrderedList)
{
node.InnerHtml = count + ". " + node.InnerHtml + "\n";
count++;
}
else
node.InnerHtml = "\u2022 " + node.InnerHtml + "\n";
break;
case "ul":
isOrderedList = false;
break;
case "ol":
count = 1;
isOrderedList = true;
break;
}
// Add to the array of attributes
switch (node.Name)
{
case "#document":
#if __ANDROID__
node_style = new FontStyle (Font, this);
#endif
#if __IOS__
node_style = new FontStyle(FontName, this);
#endif
break;
case "h1":
node_style = new HeaderStyle(1.40f, this);
break;
case "h2":
node_style = new HeaderStyle(1.30f, this);
break;
case "h3":
node_style = new HeaderStyle(1.20f, this);
break;
case "h4":
node_style = new HeaderStyle(1.10f, this);
break;
case "h5":
node_style = new HeaderStyle(1.08f, this);
break;
case "a":
node_style = new LinkStyle(node.GetAttributeValue("href", ""), this);
break;
case "img":
node_style = new ImageStyle(node.GetAttributeValue("src", ""), this);
break;
case "i":
case "em":
if (HasParent(node, "b", "strong"))
node_style = new BoldItalicStyle(this);
else
node_style = new ItalicStyle(this);
break;
case "b":
case "strong":
if (HasParent(node, "i", "em"))
node_style = new BoldItalicStyle(this);
else
node_style = new BoldStyle(this);
break;
case "font":
foreach (var attr in node.Attributes)
{
switch (attr.Name)
{
case "font":
#if __ANDROID__
node_style = new FontStyle(Font, this);
#endif
#if __IOS__
node_style = new FontStyle(FontName, this);
#endif
break;
case "color":
node_style = new ColorStyle(ReColor.Parse(attr.Value), this);
break;
}
}
break;
}
foreach (var child in node.ChildNodes)
{
switch (child.NodeType)
{
case HtmlNodeType.Comment:
break;
case HtmlNodeType.Element:
case HtmlNodeType.Document:
var child_text = EmitTextWithAttrs(child, fontSize);
foreach (var ranged_attrs in child_text.Styles)
{
// Shift the range forward
ranged_attrs.Shift(node_text.Text.Length);
node_text.Styles.Push(ranged_attrs);
}
node_text.Text += child_text.Text;
break;
case HtmlNodeType.Text:
node_text.Text += child.InnerText;
break;
}
}
// Add the attributes now that we know the text length
if (node_style != null)
{
node_text.Styles.Push(new StyleWithRange()
{
Offset = 0,
Length = node_text.Text.Length,
Style = node_style
});
}
return node_text;
}
partial class TextStyle
{
protected ReText Text;
public TextStyle(ReText text)
{
Text = text;
}
}
partial class HeaderStyle : TextStyle
{
public HeaderStyle(float factor, ReText text) : base(text)
{
Factor = factor;
}
public float Factor { get; set; }
}
partial class ImageStyle : TextStyle
{
public ImageStyle(string src, ReText text) : base(text)
{
Src = src;
}
public string Src { get; set; }
}
partial class LinkStyle : TextStyle
{
public LinkStyle(string href, ReText text) : base(text)
{
Href = href;
}
public string Href { get; set; }
}
partial class ItalicStyle : TextStyle
{
public ItalicStyle(ReText text) : base(text) { }
}
partial class BoldStyle : TextStyle
{
public BoldStyle(ReText text) : base(text) { }
}
partial class BoldItalicStyle : TextStyle
{
public BoldItalicStyle(ReText text) : base(text) { }
}
partial class ColorStyle : TextStyle
{
ReColor Color;
public ColorStyle(ReColor color, ReText text) : base(text)
{
Color = color;
}
}
partial class FontStyle : TextStyle
{
#if __ANDROID__
protected Typeface Font;
public FontStyle (Typeface font, ReText text) : base (text) {
Font = font;
}
#endif
#if __IOS__
protected string FontName;
public FontStyle(string fName, ReText text) : base(text)
{
FontName = fName;
}
#endif
}
}
}