/
LanguageTypeAdapter.cs
40 lines (32 loc) · 1.18 KB
/
LanguageTypeAdapter.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
using System.Collections.Generic;
using System.Text.RegularExpressions;
using ColorCode;
namespace Markdig.SyntaxHighlighting {
public class LanguageTypeAdapter {
private readonly Dictionary<string, ILanguage> languageMap = new Dictionary<string, ILanguage> {
{"csharp", Languages.CSharp},
{"cplusplus", Languages.Cpp}
};
public ILanguage Parse(string id, string firstLine = null) {
if (id == null) {
return null;
}
if (languageMap.ContainsKey(id)) {
return languageMap[id];
}
if (!string.IsNullOrWhiteSpace(firstLine)) {
foreach (var lang in Languages.All) {
if (lang.FirstLinePattern == null) {
continue;
}
var firstLineMatcher = new Regex(lang.FirstLinePattern, RegexOptions.IgnoreCase);
if (firstLineMatcher.IsMatch(firstLine)) {
return lang;
}
}
}
var byIdCanidate = Languages.FindById(id);
return byIdCanidate;
}
}
}