Exemplo n.º 1
0
 private void ProcessTokenOnPairMatching(PairToken pairToken, Stack <PairToken> openingTokens,
                                         LinkedList <IToken> listToAdd)
 {
     if (pairToken.IsClosing && openingTokens.Count > 0 && !openingTokens.Peek().IsClosing)
     {
         var openToken = openingTokens.Pop();
         if (openToken.Type == pairToken.Type)
         {
             AddPairTokenToLinkedList(listToAdd, openToken);
             AddPairTokenToLinkedList(listToAdd, pairToken);
         }
         else
         {
             if (openToken.Type != AttributeType.Emphasis)
             {
                 openingTokens.Push(openToken);
             }
             else
             {
                 ProcessTokenOnPairMatching(pairToken, openingTokens, listToAdd);
             }
         }
     }
     else
     {
         openingTokens.Push(pairToken);
     }
 }
Exemplo n.º 2
0
        private Token GetCorrectPairToken(PairToken token, Token prevToken, Token nextToken)
        {
            if (IsFirstPairToken(prevToken, nextToken))
            {
                return(new PairToken(token.Type, token.Content, token.Length, true));
            }

            if (IsSecondPairToken(prevToken, nextToken))
            {
                return(new PairToken(token.Type, token.Content, token.Length, false));
            }

            return(new Token(TokenType.Text, token.Content, token.Length));
        }
Exemplo n.º 3
0
        private IEnumerable <IToken> MergeAdjacentEmphasisDelimiters(IEnumerable <IToken> tokens)
        {
            var       resultTokens              = new LinkedList <IToken>();
            var       openingTokens             = new Stack <PairToken>();
            var       nonMergeableOpeningTokens = new Stack <PairToken>();
            PairToken lastOpeningToken          = null;
            PairToken lastClosingToken          = null;

            foreach (var token in tokens)
            {
                if (token is PairToken pairToken && token.Type == AttributeType.Emphasis)
                {
                    if (!pairToken.IsClosing)
                    {
                        if (lastOpeningToken == null)
                        {
                            lastOpeningToken = pairToken;
                        }
                        else
                        {
                            lastOpeningToken = TryMergeOpeningTokens(
                                openingTokens,
                                nonMergeableOpeningTokens,
                                pairToken,
                                lastOpeningToken)
                                ? null
                                : pairToken;
                        }
                    }
                    else
                    {
                        ProcessClosingTokenForMerging(
                            resultTokens,
                            nonMergeableOpeningTokens,
                            openingTokens,
                            pairToken,
                            ref lastOpeningToken,
                            ref lastClosingToken);
                    }
                }