private NumberingId GetListType(ParaPattern pattern) { if (pattern == ParaPattern.OrderedList) { return new NumberingId() { Val = 2 } } ; else { return new NumberingId() { Val = 1 } }; }
/// <summary> /// This method allows you to rebuild a paragraph without interpreting breaking lines if there is not a double space before. /// Produces a counter in order to know how many lines are concatenated, in order to delete it after parsing it /// </summary> /// <param name="mdText">The text to parse line by line</param> /// <param name="pattern">The pattern to detect real new lines. Allows to create a block with same nature (lists, paragraphs...)</param> /// <returns></returns> private static (int counter, string textBlock) BuildWithoutBreakingLines(string mdText, ParaPattern pattern) { var lines = Regex.Split(mdText, "\r\n|\r|\n").ToArray(); var previousLine = lines.First().TrimStart('>'); var output = new StringBuilder(previousLine); int count = 1; if (string.IsNullOrEmpty(previousLine)) { return(counter : count, textBlock : output.ToString()); } foreach (var line in lines.Skip(1)) { // Break directly if processed line is empty if (string.IsNullOrEmpty(line)) { break; } var linePattern = PatternMatcher.GetMarkdownMatch(line).Key; // If first pattern is a table, do not break until pattern does not match to any table pattern bool isTableContinuing = pattern == ParaPattern.Table && ( linePattern == ParaPattern.Table || linePattern == ParaPattern.TableHeaderSeparation); // Quotes are continuing if first pattern is quote // And the current one equals to Quote or AnyChar bool isQuoteContinuing = pattern == ParaPattern.Quote && (linePattern == ParaPattern.Quote || linePattern == ParaPattern.AnyChar); // Paragraph types are continuing if previous line does not ends with double space // And first pattern matches with the current one bool isParagraphContinuing = (!previousLine.EndsWith(" ") && (pattern == ParaPattern.AnyChar && linePattern == pattern)); // If list is not continuing, then check if item is detected as a code block bool isListContinuing = (pattern == ParaPattern.OrderedList || pattern == ParaPattern.UnorderedList) && (linePattern == ParaPattern.OrderedList || linePattern == ParaPattern.UnorderedList); if ((pattern == ParaPattern.OrderedList || pattern == ParaPattern.UnorderedList) && !isListContinuing && linePattern == ParaPattern.CodeBlock) { try { linePattern = PatternMatcher.GetMatchFromPattern(line, ParaPattern.OrderedList).Key; } catch (Exception e) { linePattern = PatternMatcher.GetMatchFromPattern(line, ParaPattern.UnorderedList).Key; } isListContinuing = (linePattern == ParaPattern.OrderedList || linePattern == ParaPattern.UnorderedList); } if (!isTableContinuing && !isQuoteContinuing && !isParagraphContinuing && !isListContinuing) { break; } if (pattern == ParaPattern.TableHeaderSeparation || pattern == ParaPattern.Table || pattern == ParaPattern.OrderedList || pattern == ParaPattern.UnorderedList || pattern == ParaPattern.Quote) { output.AppendLine().Append(line.TrimStart('>')); } else { output.Append(line); } previousLine = line; count++; } return(counter : count, textBlock : output.ToString()); }
public static Regex GetRegexFromPattern(ParaPattern pattern) { return(ParagraphPatterns[pattern]); }
public static KeyValuePair <ParaPattern, Match> GetMatchFromPattern(string markdown, ParaPattern pattern) { var regex = ParagraphPatterns[pattern]; if (!regex.IsMatch(markdown)) { throw new AmbiguousMatchException("The match does not refer to the needed."); } return(new KeyValuePair <ParaPattern, Match>(pattern, regex.Match(markdown))); }