-
Notifications
You must be signed in to change notification settings - Fork 1
/
HighlightJSHighlighter.cs
192 lines (157 loc) · 6.35 KB
/
HighlightJSHighlighter.cs
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
using System.Collections.Generic;
using System.Linq;
using AngleSharp;
using AngleSharp.Dom;
using System.Drawing;
using System.Globalization;
using AngleSharp.Css.Dom;
using System;
namespace PowerSyntax
{
public class HighlightJSHighlighter : ISyntaxHighlighter, IDisposable
{
public HighlightJSHighlighter(IResourceProvider resource)
{
this.resource = resource;
themes = resource.GetFiles("/styles", "*.css").Select(x => x.Split('.').Reverse().Skip(1).First()).ToArray();
highlightJS = new HighlightJS(resource);
var config = Configuration.Default.WithCss();
using (var context = BrowsingContext.New(config) as BrowsingContext)
{
context.OpenAsync(req => req.Content("<html></html>"));
}
}
public void Dispose()
{
highlightJS.Dispose();
}
public IList<string> Language => highlightJS.ListLanguages();
public IList<string> Themes => themes;
#region BrowserEngine
static StyleRange ExtractStyle(ICssStyleDeclaration style)
{
var mappedStyle = new StyleRange();
string cssColor = null;
try
{
cssColor = style?.GetPropertyValue("color");
mappedStyle.Color = ParseColor(cssColor);
}
catch (NullReferenceException) { }
try
{
string fontWeight = null;
fontWeight = style?.GetPropertyValue("font-weight");
mappedStyle.Bold = (!string.IsNullOrEmpty(fontWeight) && fontWeight == "bold");
}
catch (NullReferenceException) { }
try
{
string fontStyle = null;
fontStyle = style?.GetPropertyValue("font-style");
mappedStyle.Italic = (!string.IsNullOrEmpty(fontStyle) && fontStyle == "italic");
}
catch (NullReferenceException) { }
return mappedStyle;
}
private StyleRange Parse(int begin, IElement element, IWindow browsingWindow)
{
StyleRange style;
try
{
style = ExtractStyle(browsingWindow.GetComputedStyle(element));
}
catch (NullReferenceException)
{
style = new StyleRange();
}
style.Begin = begin;
style.Length = element.TextContent.Length;
style.Text = element.TextContent;
style.Children = Parse(begin, element.ChildNodes, browsingWindow);
return style;
}
private IEnumerable<StyleRange> Parse(int begin, INodeList nodes, IWindow browsingWindow)
{
var children = new List<StyleRange>();
int sourcePosition = begin;
foreach (var node in nodes)
{
if (node is IElement element)
{
children.Add(Parse(sourcePosition, element, browsingWindow));
}
sourcePosition += node.TextContent.Length;
}
return children;
}
public StyleRange Parse(string code, string language, string theme)
{
string highlighted = ApplyHighlight(code, language);
string css = resource.ReadAllText($"/styles/{theme}.css");
var source = $"<html><style type=\"text/css\">{css}</style></head><body><div class=\"hljs\">{highlighted}</div></body></html>";
var config = Configuration.Default.WithCss();
using (var context = BrowsingContext.New(config) as BrowsingContext)
{
var task = context.OpenAsync(req => req.Content(source));
task.Wait();
var document = task.Result;
var hljsbody = document.Body.Children.First();
var style = Parse(0, hljsbody, document.DefaultView);
string cssColor = null;
try
{
var cssStyle = document.DefaultView.GetComputedStyle(hljsbody);
cssColor = cssStyle?.GetPropertyValue("background");
}
catch (NullReferenceException) { }
style.BackGroundColor = ParseColor(cssColor);
return style;
}
}
private static Color? ParseColor(string cssColor)
{
if (string.IsNullOrEmpty(cssColor))
return null;
try
{
cssColor = cssColor.Trim();
if (cssColor.Contains("rgb")) //rgb or argb
{
int left = cssColor.IndexOf('(');
int right = cssColor.IndexOf(')');
if (left < 0 || right < 0)
throw null;
string noBrackets = cssColor.Substring(left + 1, right - left - 1);
string[] parts = noBrackets.Split(',');
int r = int.Parse(parts[0], CultureInfo.InvariantCulture);
int g = int.Parse(parts[1], CultureInfo.InvariantCulture);
int b = int.Parse(parts[2], CultureInfo.InvariantCulture);
if (parts.Length == 3)
{
return Color.FromArgb(r, g, b);
}
else if (parts.Length == 4)
{
float a = float.Parse(parts[3], CultureInfo.InvariantCulture);
return Color.FromArgb((int)(a * 255), r, g, b);
}
return null;
}
return ColorTranslator.FromHtml(cssColor);
}
catch (FormatException)
{
}
return null;
}
#endregion
private string ApplyHighlight(string code, string language)
{
return string.IsNullOrEmpty(code) ? string.Empty : highlightJS.HighlightAuto(code, language);
}
private HighlightJS highlightJS = null;
private string[] themes = null;
private IResourceProvider resource = null;
}
}