public virtual IMarkdownToken TryMatch(IMarkdownParser engine, ref string source)
        {
            var match = Blockquote.Match(source);

            if (match.Length == 0)
            {
                return(null);
            }
            source = source.Substring(match.Length);
            var capStr      = LeadingBlockquote.Replace(match.Value, string.Empty);
            var blockTokens = engine.Tokenize(capStr);

            blockTokens = TokenHelper.ParseInlineToken(engine, this, blockTokens, true);
            return(new MarkdownBlockquoteBlockToken(this, engine.Context, blockTokens, match.Value));
        }
Ejemplo n.º 2
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, ref string source)
        {
            var match = Blockquote.Match(source);

            if (match.Length == 0)
            {
                return(null);
            }
            source = source.Substring(match.Length);
            return(new TwoPhaseBlockToken(this, parser.Context, match.Value, (p, t) =>
            {
                var capStr = LeadingBlockquote.Replace(t.RawMarkdown, string.Empty);
                var blockTokens = p.Tokenize(capStr);
                blockTokens = TokenHelper.ParseInlineToken(p, t.Rule, blockTokens, true);
                return new MarkdownBlockquoteBlockToken(t.Rule, t.Context, blockTokens, match.Value);
            }));
        }
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Blockquote.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new TwoPhaseBlockToken(
                       this,
                       parser.Context,
                       sourceInfo,
                       (p, t) =>
            {
                var capStr = LeadingBlockquote.Replace(t.SourceInfo.Markdown, string.Empty);
                var blockTokens = p.Tokenize(t.SourceInfo.Copy(capStr));
                blockTokens = TokenHelper.ParseInlineToken(p, t.Rule, blockTokens, true, t.SourceInfo);
                return new MarkdownBlockquoteBlockToken(t.Rule, t.Context, blockTokens, t.SourceInfo);
            }));
        }
Ejemplo n.º 4
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, ref string source)
        {
            var match = List.Match(source);

            if (match.Length == 0)
            {
                return(null);
            }
            source = source.Substring(match.Length);

            var bull = match.Groups[2].Value;

            var cap = match.Groups[0].Value.Match(Item);

            var next   = false;
            var l      = cap.Length;
            int i      = 0;
            var tokens = new List <IMarkdownToken>();

            for (; i < l; i++)
            {
                var item = cap[i];

                // Remove the list item's bullet
                // so it is seen as the next token.
                var space = item.Length;
                item = item.ReplaceRegex(Regexes.Lexers.LeadingBullet, string.Empty);

                // Outdent whatever the
                // list item contains. Hacky.
                if (item.IndexOf("\n ") > -1)
                {
                    space -= item.Length;
                    item   = !parser.Options.Pedantic
                      ? Regex.Replace(item, "^ {1," + space + "}", "", RegexOptions.Multiline)
                      : Regex.Replace(item, @"^ {1,4}", "", RegexOptions.Multiline);
                }

                // Determine whether the next list item belongs here.
                // Backpedal if it does not belong in this list.
                if (parser.Options.SmartLists && i != l - 1)
                {
                    var b = Bullet.Apply(cap[i + 1])[0]; // !!!!!!!!!!!
                    if (bull != b && !(bull.Length > 1 && b.Length > 1))
                    {
                        source = string.Join("\n", cap.Skip(i + 1)) + source;
                        i      = l - 1;
                    }
                }

                // Determine whether item is loose or not.
                // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
                // for discount behavior.
                var loose = next || Regex.IsMatch(item, @"\n\n(?!\s*$)");
                if (i != l - 1 && item.Length != 0)
                {
                    next = item[item.Length - 1] == '\n';
                    if (!loose)
                    {
                        loose = next;
                    }
                }

                tokens.Add(
                    new TwoPhaseBlockToken(this, parser.Context, item, (p, t) =>
                {
                    p.SwitchContext(MarkdownBlockContext.IsTop, false);
                    var blockTokens = p.Tokenize(item);
                    blockTokens     = TokenHelper.ParseInlineToken(p, this, blockTokens, loose);
                    return(new MarkdownListItemBlockToken(t.Rule, t.Context, blockTokens, loose, t.RawMarkdown));
                }));
            }

            return(new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), bull.Length > 1, match.Value));
        }
Ejemplo n.º 5
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = OrderList.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                match = UnorderList.Match(context.CurrentMarkdown);
                if (match.Length == 0)
                {
                    return(null);
                }
            }
            var sourceInfo = context.Consume(match.Length);

            var bull = match.Groups[2].Value;

            var cap = match.Groups[0].Value.Match(Item);

            var next       = false;
            var l          = cap.Length;
            int i          = 0;
            var tokens     = new List <IMarkdownToken>();
            var lineOffset = 0;
            var lines      = 0;

            for (; i < l; i++)
            {
                var item = cap[i];
                lines = CountLine(item);
                // Remove the list item's bullet
                // so it is seen as the next token.
                var space = item.Length;
                item = item.ReplaceRegex(Regexes.Lexers.LeadingBullet, string.Empty);

                // Outdent whatever the
                // list item contains. Hacky.
                if (item.IndexOf("\n ") > -1)
                {
                    space -= item.Length;
                    item   = !parser.Options.Pedantic
                      ? Regex.Replace(item, "^ {1," + space + "}", "", RegexOptions.Multiline)
                      : Regex.Replace(item, @"^ {1,4}", "", RegexOptions.Multiline);
                }

                // Determine whether item is loose or not.
                // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
                // for discount behavior.
                var loose = next || Regex.IsMatch(item, @"\n\n(?!\s*$)");
                if (i != l - 1 && item.Length != 0)
                {
                    next = item[item.Length - 1] == '\n';
                    if (!loose)
                    {
                        loose = next;
                    }
                }

                tokens.Add(
                    new TwoPhaseBlockToken(
                        this,
                        parser.Context,
                        sourceInfo.Copy(item, lineOffset),
                        (p, t) =>
                {
                    p.SwitchContext(MarkdownBlockContext.IsTop, false);
                    var blockTokens = p.Tokenize(t.SourceInfo.Copy(item));
                    blockTokens     = TokenHelper.ParseInlineToken(p, this, blockTokens, loose, t.SourceInfo);
                    return(new MarkdownListItemBlockToken(t.Rule, t.Context, blockTokens, loose, t.SourceInfo));
                }));
                lineOffset += lines;
            }

            return(new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), bull.Length > 1, sourceInfo));
        }