Пример #1
0
        private void AddNewDelimiter(Substring lexeme)
        {
            var canBeClosing  = specification.CanBeClosing(lexeme, markdownInput);
            var canBeStarting = specification.CanBeStarting(lexeme, markdownInput);
            var delimiter     = new Delimiter(lexeme.Value, lexeme.Index, canBeClosing, canBeStarting);

            if (delimiter.CanBeClosing)
            {
                var closed = TryCloseToken(delimiter, out var closedToken);
                if (closed)
                {
                    ResolveToken(closedToken);
                    CurrentToken = closedToken.ParentToken;
                    return;
                }
            }

            if (delimiter.CanBeStarting)
            {
                var newToken = new Token(delimiter);
                CurrentToken.AddToken(newToken);
                CurrentToken = newToken;
            }
            else
            {
                CurrentToken.AddText(delimiter.Value);
            }
        }
Пример #2
0
        private bool DelimitersArePair(Delimiter startingDelimiter, Delimiter closingDelimiter)
        {
            if (specification.MdStartingToClosingDelimiters.ContainsKey(startingDelimiter.Value))
            {
                return(specification.MdStartingToClosingDelimiters[startingDelimiter.Value] == closingDelimiter.Value);
            }

            return(false);
        }
Пример #3
0
 public Delimiter Escape(Delimiter delimiter, string text)
 {
     if (delimiter.Position == 0)
     {
         return(delimiter);
     }
     return(text[delimiter.Position - 1] == '\\'
                ? Length != 1 ? new Delimiter(fullTag.Substring(1), delimiter.Position + 1) : null
                : delimiter);
 }
Пример #4
0
        public Token GetToken(Delimiter delimiter, string text)
        {
            if (delimiter.IsClosing)
            {
                return(null);
            }
            var second = delimiter.Partner;
            var length = second.Position - delimiter.Position + Length;

            return(new PairedTagToken(delimiter.Position, length, text.Substring(delimiter.Position, length), fullTag));
        }
Пример #5
0
        internal IEnumerable <Token> GetTokensFromDelimiters()
        {
            if (!Delimiters.Any())
            {
                return new List <Token> {
                           new StringToken(0, Text.Length, Text)
                }
            }
            ;

            var       tokens             = new LinkedList <Token>();
            Token     currentParentToken = null;
            Delimiter endOfParent        = null;

            foreach (var delimiter in Delimiters)
            {
                var rule = GetSuitableRule(delimiter);

                var token = rule.GetToken(delimiter, Text);

                if (token != null)
                {
                    if (currentParentToken == null)
                    {
                        currentParentToken = token;
                        endOfParent        = delimiter.Partner;
                        tokens.AddLast(token);
                    }

                    if (delimiter.Partner.Position >= endOfParent.Position)
                    {
                        continue;
                    }
                    token.ParentToken = currentParentToken;
                    if (currentParentToken.InnerTokens == null)
                    {
                        currentParentToken.InnerTokens = new List <Token>();
                    }
                    currentParentToken.InnerTokens.Add(token);
                }
                else if (currentParentToken != null)
                {
                    if (delimiter.Position < endOfParent.Position)
                    {
                        continue;
                    }
                    currentParentToken = null;
                    endOfParent        = null;
                }
            }

            return(InsertStringTokens(tokens));
        }
Пример #6
0
        public bool IsValidClosing(Delimiter delimiter, string text)
        {
            var position = delimiter.Position;

            if (position == 0)
            {
                return(false);
            }
            if (position + Length == text.Length)
            {
                return(!char.IsWhiteSpace(text[position - 1]));
            }
            return(!char.IsWhiteSpace(text[position - 1]) && char.IsWhiteSpace(text[position + Length]));
        }
Пример #7
0
        public bool IsValid(Delimiter delimiter, string text)
        {
            var position = delimiter.Position;
            var isLast   = position == text.Length - Length;
            var isFirst  = position == 0;

            if (isLast || isFirst)
            {
                return(true);
            }
            var next     = text[position + Length];
            var previous = text[position - 1];

            return(!(next.IsLetterOrDigitOrSpecifiedChar(Tag) && previous.IsLetterOrDigitOrSpecifiedChar(Tag)));
        }
Пример #8
0
        private bool TryCloseToken(Delimiter closingDelimiter, out Token closedToken)
        {
            closedToken = null;
            var token = CurrentToken;

            if (token.Delimiter is null)
            {
                return(false);
            }
            while (token != null && !DelimitersArePair(token.Delimiter, closingDelimiter))
            {
                if (token.ParentToken?.Delimiter is null)
                {
                    return(false);
                }

                token = token.ParentToken;
            }

            token.Closed    = true;
            token.TokenType = specification.MdToTokenTypes[token.Delimiter.Value];
            closedToken     = token;
            return(true);
        }
Пример #9
0
 internal ITextProcessorRule GetSuitableRule(Delimiter delimiter)
 {
     return(rules.FirstOrDefault(r => r.Check(delimiter)));
 }
Пример #10
0
 public Token(Delimiter delimiter)
 {
     Delimiter = delimiter;
     RootToken = this;
 }
Пример #11
0
 public bool Check(Delimiter delimiter) => delimiter.Value == fullTag;