|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 |
| - |
| 3 | +using System.Linq; |
4 | 4 | using System.Globalization;
|
| 5 | +using System.Text.RegularExpressions; |
5 | 6 | using Newtonsoft.Json;
|
6 | 7 | using Newtonsoft.Json.Converters;
|
7 | 8 | namespace graphql
|
@@ -29,14 +30,50 @@ public partial class QuestionDetail
|
29 | 30 | [JsonProperty("codeDefinition")]
|
30 | 31 | public string CodeDefinition { get; set; }
|
31 | 32 |
|
| 33 | + [JsonProperty("sampleTestCase")] |
| 34 | + public string SampleTestCase {get; set;} |
| 35 | + |
| 36 | + [JsonProperty("metaData")] |
| 37 | + public string MetaData {get; set;} |
| 38 | + |
32 | 39 | [JsonProperty("questionDetailUrl")]
|
33 | 40 | public string QuestionDetailUrl{get;set;}
|
| 41 | + |
| 42 | + public string QuestionName => QuestionTitle.Trim().Replace(" ", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace("'", "").Replace("-", ""); |
| 43 | + public string QuestionUrl => $"{Leetcode.BaseUrl}{QuestionDetailUrl}"; |
34 | 44 | public List<CodeDefinition> CodeDefinitions => JsonConvert.DeserializeObject<List<CodeDefinition>>(CodeDefinition);
|
| 45 | + |
| 46 | + public string[] ContentLines => HtmlToText(Content).Split('\n'); |
| 47 | + public MethodData MethodData => JsonConvert.DeserializeObject<MethodData>(MetaData); |
| 48 | + |
| 49 | + public string CSharpCode => CodeDefinitions.Where(x=>x.Value=="csharp").First().DefaultCode; |
35 | 50 | }
|
36 | 51 |
|
37 | 52 | public partial class QuestionDetail
|
38 | 53 | {
|
39 | 54 | public static QuestionDetail FromJson(string json) => JsonConvert.DeserializeObject<QuestionDetail>(json, Converter.Settings);
|
| 55 | + |
| 56 | + private static string HtmlToText(string html) |
| 57 | + { |
| 58 | + const string tagWhiteSpace = @"(>|$)(\W|\n|\r)+<";//matches one or more (white space or line breaks) between '>' and '<' |
| 59 | + const string stripFormatting = @"<[^>]*(>|$)";//match any character between '<' and '>', even when end tag is missing |
| 60 | + const string lineBreak = @"<(br|BR)\s{0,1}\/{0,1}>";//matches: <br>,<br/>,<br />,<BR>,<BR/>,<BR /> |
| 61 | + var lineBreakRegex = new Regex(lineBreak, RegexOptions.Multiline); |
| 62 | + var stripFormattingRegex = new Regex(stripFormatting, RegexOptions.Multiline); |
| 63 | + var tagWhiteSpaceRegex = new Regex(tagWhiteSpace, RegexOptions.Multiline); |
| 64 | + |
| 65 | + var text = html; |
| 66 | + //Decode html specific characters |
| 67 | + text = System.Net.WebUtility.HtmlDecode(text); |
| 68 | + //Remove tag whitespace/line breaks |
| 69 | + text = tagWhiteSpaceRegex.Replace(text, "><"); |
| 70 | + //Replace <br /> with line breaks |
| 71 | + text = lineBreakRegex.Replace(text, Environment.NewLine); |
| 72 | + //Strip formatting |
| 73 | + text = stripFormattingRegex.Replace(text, string.Empty); |
| 74 | + |
| 75 | + return text; |
| 76 | + } |
40 | 77 | }
|
41 | 78 |
|
42 | 79 |
|
|
0 commit comments