Beispiel #1
0
        public void EmInStrong_Works()
        {
            var text  = "__t_t_t__";
            var state = new TextTagsState(text, 2, text.Length - 2);

            tagsFinder.GetFirstTagOnSegment(state).Should().Be(new TagSegment(TagName.Em, 3, 5));
        }
Beispiel #2
0
        public TagSegment GetFirstTagOnSegment(TextTagsState state)
        {
            int?    openedIndex, closedIndex;
            TagName?tagName;

            while (true)
            {
                openedIndex = GetIndexTagBracket(state, IsOpenedTag, null, false);
                if (!openedIndex.HasValue)
                {
                    return(null);
                }
                state   = state.ChangeSegment(openedIndex.Value, state.End);
                tagName = GetTagName(state, IsOpenedTag);
                var indexInTag = openedIndex.Value + Tags.GetMd(tagName.Value).Length;
                if (indexInTag > state.End)
                {
                    return(null);
                }
                state       = state.ChangeSegment(indexInTag, state.End);
                closedIndex = GetIndexTagBracket(state, IsClosedTag, tagName, true);
                if (!closedIndex.HasValue || IsEmptyInsideTag(closedIndex, openedIndex, tagName))
                {
                    continue;
                }
                break;
            }
            return(new TagSegment(tagName.Value, openedIndex.Value, closedIndex.Value));
        }
Beispiel #3
0
        public void InactiveEmInStrongEm()
        {
            var text  = "___a_a_a___";
            var state = new TextTagsState(new BitArray(new[] { true, false, false }), text, 3, text.Length - 4);

            tagsFinder.GetFirstTagOnSegment(state).Should().BeNull();
        }
Beispiel #4
0
        public void StrongInEm_DoesNotWork()
        {
            var text  = "_t__t__t_";
            var state = new TextTagsState(new BitArray(new [] { false, false, true }), text, 1, text.Length - 2);

            tagsFinder.GetFirstTagOnSegment(state).Should().Be(null);
        }
Beispiel #5
0
        private IEnumerable <TagSegment> GetAllTags(TextTagsState state)
        {
            var nextStates = new Stack <TextTagsState>();

            for (var index = 0; index < state.Text.Length; index++)
            {
                var tag = finder.GetFirstTagOnSegment(state);
                if (tag == null)
                {
                    if (nextStates.Count == 0)
                    {
                        break;
                    }
                    index = nextStates.Peek().Start - 1;
                    state = nextStates.Pop();
                    continue;
                }
                yield return(tag);

                var newStart = tag.CloseIndex + Tags.GetMd(tag.TagName).Length;
                if (newStart < state.End)
                {
                    nextStates.Push(state.ChangeSegment(newStart, state.End));
                }
                index = tag.OpenIndex + Tags.GetMd(tag.TagName).Length;
                if (index <= state.End - 1)
                {
                    state = state.SwitchTag(tag.TagName).ChangeSegment(index, tag.CloseIndex - 1);
                }
                index -= 1;
            }
        }
Beispiel #6
0
        public void FindFirstTag_InBeginingAndOutOfTags(TagName expectedTagName, int expectedX, int expectedY)
        {
            var md    = Tags.GetMd(expectedTagName);
            var text  = string.Format("{0}t{0}t", md);
            var state = new TextTagsState(text, 0, text.Length - 1);

            tagsFinder.GetFirstTagOnSegment(state).Should().Be(new TagSegment(expectedTagName, expectedX, expectedY));
        }
Beispiel #7
0
 private TagName?GetTagName(TextTagsState state, Func <TagName, TextTagsState, bool> condition)
 {
     return(Enum
            .GetValues(typeof(TagName))
            .Cast <TagName?>()
            .FirstOrDefault(tagName =>
                            condition(tagName.Value, state)));
 }
Beispiel #8
0
 private bool IsClosedTag(TagName tag, TextTagsState state)
 {
     return(IsItLooksLikeTag(tag, state) &&
            state.Start > 0 &&
            state.Text[state.Start - 1] != ' ' &&
            IsNotPrevSymbolUnderlining(state) &&
            !char.IsDigit(state.Text[state.Start - 1]));
 }
Beispiel #9
0
        public void SwitchTag_InverseBitInMask()
        {
            var expectedState = !textTagsState.IsInTag(TagName.Em);

            textTagsState = textTagsState.SwitchTag(TagName.Em);

            textTagsState.IsInTag(TagName.Em).Should().Be(expectedState);
        }
Beispiel #10
0
 private bool IsNotPrevSymbolUnderlining(TextTagsState state)
 {
     return(state.Start == 0 ||
            state.Start > 0 &&
            (state.Text[state.Start - 1] != '_' ||
             state.Start > 1 &&
             state.Text[state.Start - 2] == '\\'));
 }
Beispiel #11
0
        private bool IsOpenedTag(TagName tag, TextTagsState state)
        {
            var indexInTag = state.Start + Tags.GetMd(tag).Length;

            return(IsItLooksLikeTag(tag, state) &&
                   indexInTag - 1 < state.End &&
                   state.Text[indexInTag] != ' ' &&
                   IsNotPrevSymbolUnderlining(state) &&
                   !char.IsDigit(state.Text[indexInTag]));
        }
Beispiel #12
0
 private int?GetIndexTagBracket(
     TextTagsState state,
     Func <TagName, TextTagsState, bool> condition,
     TagName?expectedTagName,
     bool isEqual)
 {
     return(Enumerable
            .Range(state.Start, state.End - state.Start + 1)
            .Cast <int?>()
            .FirstOrDefault(i =>
                            Equals(GetTagName(state.ChangeSegment(i.Value, state.End), condition), expectedTagName) == isEqual));
 }
Beispiel #13
0
        private bool IsItLooksLikeTag(TagName tag, TextTagsState state)
        {
            const int moreThanAnyTagLength = 4;

            if (state.Start + Tags.GetMd(tag).Length - 1 > state.End || IsDisabledEscape(state))
            {
                return(false);
            }
            if (state.Start + moreThanAnyTagLength <= state.End &&
                state.Text.Substring(state.Start, moreThanAnyTagLength) == "____")
            {
                return(false);
            }
            var potentialTag = state.Text.Substring(state.Start, Tags.GetMd(tag).Length);

            return(Tags.GetMd(tag) == potentialTag &&
                   !state.IsInTag(TagName.Em) &&
                   !state.IsInTag(TagName.StrongEm));
        }
Beispiel #14
0
 public string RenderToHtml(string markdown)
 {
     state = new TextTagsState(markdown, 0, markdown.Length - 1);
     return(replacer.ReplaceTags(markdown, GetAllTags(state).ToList())); //TODO
 }
Beispiel #15
0
 public void SetUp()
 {
     textTagsState = new TextTagsState(new BitArray(new[] { false, true, false }), "", 0, 0);
 }
Beispiel #16
0
 private bool IsDisabledEscape(TextTagsState state) =>
 state.Start > 0 && state.Text[state.Start - 1] == '\\';
Beispiel #17
0
        private void TestGetFirstTagOnSegment(string text, TagSegment expectedTagSegment)
        {
            var state = new TextTagsState(text, 0, text.Length - 1);

            tagsFinder.GetFirstTagOnSegment(state).Should().Be(expectedTagSegment);
        }
Beispiel #18
0
        public TagSegment TagMustClosed(string text)
        {
            var state = new TextTagsState(text, 0, text.Length - 1);

            return(tagsFinder.GetFirstTagOnSegment(state));
        }