|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | | -using System.Collections.Immutable; |
4 | | -using System.IO.Pipes; |
5 | 3 | using System.Linq; |
6 | | -using System.Text.RegularExpressions; |
7 | 4 | using System.Threading.Tasks; |
8 | 5 | using Microsoft.AspNetCore.Html; |
| 6 | +using Microsoft.AspNetCore.Mvc.Rendering; |
9 | 7 | using Microsoft.AspNetCore.Razor.TagHelpers; |
10 | | -using Microsoft.CodeAnalysis; |
11 | 8 |
|
12 | 9 | namespace OpenTabletDriver.Web.TagHelpers |
13 | 10 | { |
14 | 11 | [HtmlTargetElement("codeblock")] |
15 | 12 | public class CodeBlockTagHelper : TagHelper |
16 | 13 | { |
17 | 14 | public string Language { set; get; } |
18 | | - |
| 15 | + |
19 | 16 | public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) |
20 | 17 | { |
21 | 18 | output.TagName = "pre"; |
22 | 19 |
|
23 | | - if (output.Attributes.TryGetAttribute("class", out var classAttr)) |
24 | | - output.Attributes.SetAttribute("class", $"{classAttr.Value} card card-body"); |
25 | | - else |
26 | | - output.Attributes.Add("class", $"card card-body"); |
27 | | - |
28 | | - |
29 | 20 | var content = await output.GetChildContentAsync(); |
30 | 21 | var innerHtml = content.GetContent().Trim('\n'); |
31 | | - |
32 | 22 | var body = TrimPreceding(innerHtml, ' '); |
33 | | - output.Content.SetHtmlContent(body); |
| 23 | + |
| 24 | + var code = new TagBuilder("code"); |
| 25 | + code.AddCssClass("hljs"); |
| 26 | + code.AddCssClass($"language-{Language}"); |
| 27 | + code.InnerHtml.SetHtmlContent(body); |
| 28 | + |
| 29 | + output.Content.SetHtmlContent(code); |
34 | 30 | } |
35 | 31 |
|
36 | 32 | private string TrimPreceding(string value, char character) |
37 | 33 | { |
38 | 34 | var lines = value.Split(Environment.NewLine); |
39 | | - int preceding = CountPreceding(lines, character); |
| 35 | + var preceding = CountPreceding(lines, character); |
40 | 36 | var trimmedLines = from line in lines |
41 | 37 | select TrimPrecedingLine(line, character, preceding); |
42 | 38 |
|
43 | | - var formattedLines = LanguageFormat(trimmedLines.ToArray(), Language); |
44 | | - |
45 | | - return string.Join(Environment.NewLine, formattedLines); |
46 | | - } |
47 | | - |
48 | | - private IEnumerable<string> LanguageFormat(IList<string> lines, string language) |
49 | | - { |
50 | | - switch (language) |
51 | | - { |
52 | | - case "sh": |
53 | | - case "bash": |
54 | | - case "nix": |
55 | | - { |
56 | | - for (int i = 0; i < lines.Count; i++) |
57 | | - { |
58 | | - var line = lines[i]; |
59 | | - if (line.TrimStart().StartsWith("#")) |
60 | | - { |
61 | | - var nextLine = lines[i + 1]; |
62 | | - const string tag = "span"; |
63 | | - yield return $"<{tag} class=\"text-muted\">{line}{Environment.NewLine}</{tag}>{nextLine}"; |
64 | | - i++; |
65 | | - } |
66 | | - else |
67 | | - { |
68 | | - yield return line; |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - break; |
73 | | - } |
74 | | - case "ini": |
75 | | - { |
76 | | - for (int i = 0; i < lines.Count; i++) |
77 | | - { |
78 | | - var line = lines[i]; |
79 | | - if (Regex.IsMatch(line, @"^\[.+?\]$")) |
80 | | - { |
81 | | - var nextLine = lines[i + 1]; |
82 | | - const string tag = "span"; |
83 | | - yield return $"<{tag} class=\"text-info\">{line}{Environment.NewLine}</{tag}>{nextLine}"; |
84 | | - i++; |
85 | | - } |
86 | | - else |
87 | | - { |
88 | | - yield return line; |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - break; |
93 | | - } |
94 | | - default: |
95 | | - { |
96 | | - foreach (var line in lines) |
97 | | - yield return line; |
98 | | - break; |
99 | | - } |
100 | | - } |
| 39 | + return string.Join(Environment.NewLine, trimmedLines); |
101 | 40 | } |
102 | 41 |
|
103 | 42 | private int CountPreceding(IEnumerable<string> lines, char leadingCharacter) |
|
0 commit comments