Beispiel #1
0
        public override bool Match(InlineProcessor processor, ref StringSlice slice)
        {
            // First, some definitions.
            // A delimiter run is a sequence of one or more delimiter characters that is not preceded or followed by the same delimiter character
            // The amount of delimiter characters in the delimiter run may exceed emphasisDesc.MaximumCount, as that is handeled in `ProcessEmphasis`

            var delimiterChar = slice.CurrentChar;
            var emphasisDesc  = emphasisMap[delimiterChar];

            char pc = (char)0;

            if (processor.Inline is HtmlEntityInline htmlEntityInline)
            {
                if (htmlEntityInline.Transcoded.Length > 0)
                {
                    pc = htmlEntityInline.Transcoded[htmlEntityInline.Transcoded.End];
                }
            }
            if (pc == 0)
            {
                pc = slice.PeekCharExtra(-1);
                if (pc == delimiterChar && slice.PeekCharExtra(-2) != '\\')
                {
                    // If we get here, we determined that either:
                    // a) there weren't enough delimiters in the delimiter run to satisfy the MinimumCount condition
                    // b) the previous character couldn't open/close
                    return(false);
                }
            }
            var startPosition = slice.Start;

            int  delimiterCount = 0;
            char c;

            do
            {
                delimiterCount++;
                c = slice.NextChar();
            } while (c == delimiterChar);


            // If the emphasis doesn't have the minimum required character
            if (delimiterCount < emphasisDesc.MinimumCount)
            {
                return(false);
            }

            // The following character is actually an entity, we need to decode it
            if (HtmlEntityParser.TryParse(ref slice, out string htmlString, out int htmlLength))
            {
                c = htmlString[0];
            }

            // Calculate Open-Close for current character
            CharHelper.CheckOpenCloseDelimiter(pc, c, emphasisDesc.EnableWithinWord, out bool canOpen, out bool canClose);

            // We have potentially an open or close emphasis
            if (canOpen || canClose)
            {
                var delimiterType = DelimiterType.Undefined;
                if (canOpen)
                {
                    delimiterType |= DelimiterType.Open;
                }
                if (canClose)
                {
                    delimiterType |= DelimiterType.Close;
                }

                var delimiter = new EmphasisDelimiterInline(this, emphasisDesc)
                {
                    DelimiterCount = delimiterCount,
                    Type           = delimiterType,
                    Span           = new SourceSpan(processor.GetSourcePosition(startPosition, out int line, out int column), processor.GetSourcePosition(slice.Start - 1)),
                    Column         = column,
                    Line           = line,
                };

                processor.Inline = delimiter;
                return(true);
            }

            // We don't have an emphasis
            return(false);
        }