private static bool AreMarksEqualed(int openedMarkIndex, int closingMarkIndex, string text) { var mark = MarkdownTags.GetMarkFromText(openedMarkIndex, text); return(text.Substring(openedMarkIndex, mark.Length) == text.Substring(closingMarkIndex - mark.Length + 1, mark.Length)); }
public void Process() { var resultBuilder = new StringBuilder(); var screened = false; while (currentIndex < text.Length || buffer.Count > 0) { switch (GetRenderStatus()) { case RenderStatus.CheckScreening: var slashCount = GetSlashesCount(); screened = slashCount % 2 != 0; resultBuilder.Append(new string('\\', slashCount - slashCount % 2)); currentIndex += slashCount; break; case RenderStatus.AppendClosingTag: AppendFromBuffer(currentIndex, resultBuilder); break; case RenderStatus.CheckMarkSymbol: var tag = TagBuilder.BuildTag(text, currentIndex); switch (GetTagStatus(tag, screened)) { case TagStatus.Screened: screened = false; buffer[tag.OpenPosition] = tag.Mark; buffer[tag.ClosePosition - tag.Mark.Length + 1] = tag.Mark; break; case TagStatus.Correct: buffer[tag.OpenPosition] = $"<{tag.TagName}>"; buffer[tag.ClosePosition - tag.Mark.Length + 1] = $"</{tag.TagName}>"; openedTags.Add(tag.TagType); break; case TagStatus.Incorrect: var mark = MarkdownTags.GetMarkFromText(currentIndex, text); resultBuilder.Append(mark); currentIndex += mark.Length; break; } break; case RenderStatus.PlainText: resultBuilder.Append(text[currentIndex]); currentIndex++; break; } } Result = resultBuilder.ToString(); }
private static bool IsTagCorrect(int startIndex, int endIndex, string text) { var mark = MarkdownTags.GetMarkFromText(startIndex, text); var tagContent = GetContent(startIndex, endIndex, text); return(mark == "#" || endIndex < text.Length && AreMarksEqualed(startIndex, endIndex, text) && !tagContent.Any(char.IsDigit) && !string.IsNullOrEmpty(tagContent) && !AreMarksInsideDifferentWords(startIndex, endIndex, text)); }
private static string GetContent(int startIndex, int endIndex, string text) { var contentBuilder = new StringBuilder(); var mark = MarkdownTags.GetMarkFromText(startIndex, text); for (var i = startIndex + mark.Length; i <= endIndex - mark.Length; i++) { contentBuilder.Append(text[i]); } return(contentBuilder.ToString()); }
public static Tag BuildTag(string text, int startIndex) { var mark = MarkdownTags.GetMarkFromText(startIndex, text); if (!MarkdownTags.IsMark(mark)) { return(Tag.EmptyOn(startIndex)); } var endPos = FindClosePosition(text, startIndex, mark); var tagName = MarkdownTags.GetHtmlTagByMark(mark); return(IsTagCorrect(startIndex, endPos, text) ? Tag.Correct(tagName, startIndex, endPos) : Tag.Incorrect(tagName, startIndex, endPos)); }
private static int FindClosePosition(string text, int startIndex, string openedMark) { var i = startIndex + 1; var pairMark = MarkdownTags.GetMarkPair(openedMark); var mark = MarkdownTags.GetMarkFromText(i, text); while (i < text.Length - openedMark.Length + 1) { mark = MarkdownTags.GetMarkFromText(i, text); if (mark == pairMark && !char.IsWhiteSpace(text[i - 1])) { break; } i += mark.Length; } return(i == text.Length ? i : i + mark.Length - 1); }
private RenderStatus GetRenderStatus() { if (buffer.ContainsKey(currentIndex)) { return(RenderStatus.AppendClosingTag); } if (MarkdownTags.ExpectedToBeMark(currentIndex, text)) { return(RenderStatus.CheckMarkSymbol); } if (currentIndex < text.Length && text[currentIndex] == '\\' && MarkdownTags.ExpectedToBeMark(currentIndex + 1, text)) { return(RenderStatus.CheckScreening); } return(RenderStatus.PlainText); }