public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context) { var c = parser.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var result = base.TryMatch(parser, context); parser.SwitchContext(c); return result; }
public override IMarkdownToken TryMatch(IMarkdownParser engine, ref string source) { var c = engine.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var result = base.TryMatch(engine, ref source); engine.SwitchContext(c); return result; }
public IMarkdownToken Extract(IMarkdownParser parser) { var c = parser.SwitchContext(Context); var result = _extractor(parser, this); parser.SwitchContext(c); return result; }
public override IMarkdownToken TryMatch(IMarkdownParser parser, ref string source) { var c = parser.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var result = base.TryMatch(parser, ref source); parser.SwitchContext(c); return(result); }
public IMarkdownToken Extract(IMarkdownParser parser) { var c = parser.SwitchContext(Context); var result = _extractor(parser, this); parser.SwitchContext(c); return(result); }
public override 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 c = engine.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var tokens = engine.Tokenize(capStr); engine.SwitchContext(c); return new AzureBlockquoteBlockToken(this, engine.Context, tokens, match.Value); }
public override 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 c = engine.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var tokens = engine.Tokenize(capStr); engine.SwitchContext(c); return(new AzureBlockquoteBlockToken(this, engine.Context, tokens, match.Value)); }
public override IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context) { var match = Blockquote.Match(context.CurrentMarkdown); if (match.Length == 0) { return(null); } var sourceInfo = context.Consume(match.Length); var capStr = LeadingBlockquote.Replace(match.Value, string.Empty); var c = engine.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var tokens = engine.Tokenize(sourceInfo.Copy(capStr)); engine.SwitchContext(c); return(new AzureBlockquoteBlockToken(this, engine.Context, tokens, sourceInfo)); }
public static InlineContent TokenizeInline(this IMarkdownParser parser, SourceInfo sourceInfo) { if (parser == null) { throw new ArgumentNullException(nameof(parser)); } if (!(parser.Context is MarkdownBlockContext context)) { throw new InvalidOperationException($"{nameof(parser)}.{nameof(parser.Context)}(type:{parser.Context.GetType().FullName}) is invalid."); } var c = parser.SwitchContext(context.GetInlineContext()); var tokens = parser.Tokenize(sourceInfo); parser.SwitchContext(c); return(new InlineContent(tokens)); }
protected virtual IMarkdownToken GenerateToken(IMarkdownParser parser, string href, string title, string text, bool isImage, SourceInfo sourceInfo, MarkdownLinkType linkType, string refId) { var escapedHref = StringHelper.UnescapeMarkdown(href); var c = parser.SwitchContext(MarkdownInlineContext.IsInLink, BoxedTrue); IMarkdownToken result; if (isImage) { result = new MarkdownImageInlineToken(this, parser.Context, escapedHref, title, text, sourceInfo, linkType, refId); } else { result = new MarkdownLinkInlineToken(this, parser.Context, escapedHref, title, parser.Tokenize(sourceInfo.Copy(text)), sourceInfo, linkType, refId); } parser.SwitchContext(c); return(result); }
public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context) { var match = Blockquote.Match(context.CurrentMarkdown); if (match.Length == 0) { return null; } var sourceInfo = context.Consume(match.Length); var c = parser.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var capStr = LeadingBlockquote.Replace(sourceInfo.Markdown, string.Empty); var blockTokens = parser.Tokenize(sourceInfo.Copy(capStr)); blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, true, sourceInfo); parser.SwitchContext(c); return new MarkdownBlockquoteBlockToken( this, parser.Context, blockTokens, sourceInfo); }
public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context) { var match = Blockquote.Match(context.CurrentMarkdown); if (match.Length == 0) { return(null); } var sourceInfo = context.Consume(match.Length); var c = parser.SwitchContext(MarkdownBlockContext.IsBlockQuote, true); var capStr = LeadingBlockquote.Replace(sourceInfo.Markdown, string.Empty); var blockTokens = parser.Tokenize(sourceInfo.Copy(capStr)); blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, true, sourceInfo); parser.SwitchContext(c); return(new MarkdownBlockquoteBlockToken( this, parser.Context, blockTokens, sourceInfo)); }
public virtual IMarkdownToken TryMatch(IMarkdownParser engine, ref string source) { var match = Tag.Match(source); if (match.Length == 0) { return null; } source = source.Substring(match.Length); var context = engine.Context; var inLink = (bool)context.Variables[MarkdownInlineContext.IsInLink]; if (!inLink && Regexes.Lexers.StartHtmlLink.IsMatch(match.Value)) { engine.SwitchContext(MarkdownInlineContext.IsInLink, true); } else if (inLink && Regexes.Lexers.EndHtmlLink.IsMatch(match.Value)) { engine.SwitchContext(MarkdownInlineContext.IsInLink, false); } return new MarkdownTagInlineToken(this, engine.Context, match.Value); }
public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context) { var match = Tag.Match(context.CurrentMarkdown); if (match.Length == 0) { return null; } var sourceInfo = context.Consume(match.Length); var c = parser.Context; var inLink = (bool)c.Variables[MarkdownInlineContext.IsInLink]; if (!inLink && Regexes.Lexers.StartHtmlLink.IsMatch(match.Value)) { parser.SwitchContext(MarkdownInlineContext.IsInLink, true); } else if (inLink && Regexes.Lexers.EndHtmlLink.IsMatch(match.Value)) { parser.SwitchContext(MarkdownInlineContext.IsInLink, false); } return new MarkdownTagInlineToken(this, parser.Context, sourceInfo); }
public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context) { var match = Tag.Match(context.CurrentMarkdown); if (match.Length == 0) { return(null); } var sourceInfo = context.Consume(match.Length); var c = parser.Context; var inLink = (bool)c.Variables[MarkdownInlineContext.IsInLink]; if (!inLink && Regexes.Lexers.StartHtmlLink.IsMatch(match.Value)) { parser.SwitchContext(MarkdownInlineContext.IsInLink, true); } else if (inLink && Regexes.Lexers.EndHtmlLink.IsMatch(match.Value)) { parser.SwitchContext(MarkdownInlineContext.IsInLink, false); } return(new MarkdownTagInlineToken(this, parser.Context, sourceInfo)); }
public virtual IMarkdownToken TryMatch(IMarkdownParser parser, ref string source) { var match = Tag.Match(source); if (match.Length == 0) { return(null); } source = source.Substring(match.Length); var context = parser.Context; var inLink = (bool)context.Variables[MarkdownInlineContext.IsInLink]; if (!inLink && Regexes.Lexers.StartHtmlLink.IsMatch(match.Value)) { parser.SwitchContext(MarkdownInlineContext.IsInLink, true); } else if (inLink && Regexes.Lexers.EndHtmlLink.IsMatch(match.Value)) { parser.SwitchContext(MarkdownInlineContext.IsInLink, false); } return(new MarkdownTagInlineToken(this, parser.Context, match.Value)); }
public static IMarkdownContext SwitchContext(this IMarkdownParser parser, string variableKey, object value) { if (parser == null) { throw new ArgumentNullException(nameof(parser)); } if (variableKey == null) { throw new ArgumentNullException(nameof(variableKey)); } return(parser.SwitchContext( parser.Context.CreateContext( parser.Context.Variables.SetItem(variableKey, value)))); }
public static IMarkdownContext SwitchContext(this IMarkdownParser parser, IReadOnlyDictionary <string, object> variables) { if (parser == null) { throw new ArgumentNullException(nameof(parser)); } if (variables == null) { throw new ArgumentNullException(nameof(variables)); } var builder = parser.Context.Variables.ToBuilder(); foreach (var pair in variables) { builder[pair.Key] = pair.Value; } return(parser.SwitchContext( parser.Context.CreateContext( builder.ToImmutable()))); }
public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context) { if (OrderList != Regexes.Block.OrderList || UnorderList != Regexes.Block.UnorderList) { return(TryMatchOld(parser, context)); } var match = context.Match(OrderListMatcher); int start = 1; bool ordered; if (match == null) { match = context.Match(UnorderListMatcher); ordered = false; } else { start = int.Parse(match["start"].GetValue()); ordered = true; } if (match?.Length > 0) { var sourceInfo = context.Consume(match.Length); var cap = sourceInfo.Markdown.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; } } var c = parser.SwitchContext(MarkdownBlockContext.IsTop, false); var itemSourceInfo = sourceInfo.Copy(item, lineOffset); var blockTokens = parser.Tokenize(itemSourceInfo); parser.SwitchContext(c); blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, loose, itemSourceInfo); tokens.Add(new MarkdownListItemBlockToken(this, parser.Context, blockTokens, loose, itemSourceInfo)); lineOffset += lines; } return(new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), ordered, start, sourceInfo)); } return(null); }
public virtual IMarkdownToken TryMatch(IMarkdownParser parser, ref string source) { var match = Regexes.Block.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; } } var c = parser.SwitchContext(MarkdownBlockContext.IsTop, false); var blockTokens = parser.Tokenize(item); blockTokens = TokenHelper.ParseInlineToken(parser, this, blockTokens, loose); tokens.Add(new MarkdownListItemBlockToken(this, parser.Context, blockTokens, loose, item)); parser.SwitchContext(c); } return(new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), bull.Length > 1, match.Value)); }
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; } var c = parser.SwitchContext(MarkdownBlockContext.IsTop, false); if (!loose) { var bc = (MarkdownBlockContext)parser.Context; c = parser.SwitchContext( bc.SetRules( ImmutableList.Create<IMarkdownRule>( this, new MarkdownNewLineBlockRule(), new MarkdownTextBlockRule()))); } var itemSourceInfo = sourceInfo.Copy(item, lineOffset); var blockTokens = parser.Tokenize(itemSourceInfo); parser.SwitchContext(c); blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, loose, itemSourceInfo); tokens.Add(new MarkdownListItemBlockToken(this, parser.Context, blockTokens, loose, itemSourceInfo)); lineOffset += lines; } return new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), bull.Length > 1, sourceInfo); }
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; } } var c = parser.SwitchContext(MarkdownBlockContext.IsTop, false); if (!loose) { var bc = (MarkdownBlockContext)parser.Context; c = parser.SwitchContext( bc.SetRules( ImmutableList.Create <IMarkdownRule>( this, new MarkdownNewLineBlockRule(), new MarkdownTextBlockRule()))); } var itemSourceInfo = sourceInfo.Copy(item, lineOffset); var blockTokens = parser.Tokenize(itemSourceInfo); parser.SwitchContext(c); blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, loose, itemSourceInfo); tokens.Add(new MarkdownListItemBlockToken(this, parser.Context, blockTokens, loose, itemSourceInfo)); lineOffset += lines; } return(new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), bull.Length > 1, sourceInfo)); }
public virtual IMarkdownToken TryMatch(IMarkdownParser parser, ref string source) { var match = Regexes.Block.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; } var c = parser.SwitchContext(MarkdownBlockContext.IsTop, false); var blockTokens = parser.Tokenize(item); blockTokens = TokenHelper.ParseInlineToken(parser, this, blockTokens, loose); tokens.Add(new MarkdownListItemBlockToken(this, parser.Context, blockTokens, loose, item)); parser.SwitchContext(c); } return new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), bull.Length > 1, match.Value); }