-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectToTableTagHelper.cs
More file actions
34 lines (32 loc) · 1.27 KB
/
ObjectToTableTagHelper.cs
File metadata and controls
34 lines (32 loc) · 1.27 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
using System.Collections.Generic;
using System.Security.AccessControl;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.AspNet.Razor.TagHelpers;
using Newtonsoft.Json;
namespace ObjectToTableHelper
{
[HtmlTargetElement("object-to-table", TagStructure = TagStructure.Unspecified)]
public class ObjectToTableTagHelper : TagHelper
{
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "table";
var c = await output.GetChildContentAsync();
var input = c.GetContent().Replace(""", "\"");
Regex r = new Regex("({|,)\\D(?<propName>\\w+)\\D:\\D(?<value>[^\"]+)\\D");
var matches = r.Matches(input);
var outputBuilder = new StringBuilder();
foreach (Match match in matches)
{
var prop = match.Groups["propName"];
var value = match.Groups["value"];
outputBuilder.Append($"<tr><td>{prop.Value}</td><td>{value.Value}</td><tr>");
}
output.Content.SetContent(new HtmlString(outputBuilder.ToString()));
}
}
}