public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context) { var match = AzureIncludeRegex.Match(context.CurrentMarkdown); if (match.Length == 0) { return(null); } var sourceInfo = context.Consume(match.Length); // [!azure.include[title](path "optionalTitle")] // 1. Get include file path var path = match.Groups[2].Value; // 2. Get title var value = match.Groups[1].Value; var title = match.Groups[4].Value; if (!TypeForwardedToPathUtility.IsRelativePath(path)) { Logger.LogWarning($"Azure inline include path {path} is not a relative path, can't expand it"); return(new MarkdownTextToken(this, parser.Context, match.Value, sourceInfo)); } object currentFilePath; if (!parser.Context.Variables.TryGetValue("path", out currentFilePath)) { Logger.LogWarning($"Can't get path for the file that ref azure block include file, return MarkdownTextToken. Raw: {match.Value}"); return(new MarkdownTextToken(this, parser.Context, match.Value, sourceInfo)); } var includeFilePath = TypeForwardedToPathUtility.NormalizePath(Path.Combine(Path.GetDirectoryName(currentFilePath.ToString()), path)); if (!File.Exists(includeFilePath)) { Logger.LogWarning($"Can't get include file path {includeFilePath} in the file {currentFilePath}, return MarkdownTextToken. Raw: {match.Value}"); return(new MarkdownTextToken(this, parser.Context, match.Value, sourceInfo)); } var blockTokens = parser.Tokenize(SourceInfo.Create(MarkdownEngine.Normalize(File.ReadAllText(includeFilePath)), includeFilePath)); blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, true, sourceInfo); return(new AzureIncludeBlockToken(this, parser.Context, path, value, title, blockTokens, match.Groups[0].Value, 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)); }
private static ImmutableArray <IMarkdownToken> Tokenize(string markdown, string file) { var gfm = new GfmEngineBuilder(new Options()).CreateEngine(new HtmlRenderer()); var sourceInfo = SourceInfo.Create(markdown.Replace("\r\n", "\n"), file); var tokens = gfm.Parser.Tokenize(sourceInfo); tokens = TokenHelper.CreateParagraghs( gfm.Parser, MarkdownParagraphBlockRule.Instance, tokens, true, sourceInfo); var rewriter = new MarkdownRewriteEngine( gfm, MarkdownTokenRewriterFactory.FromLambda <IMarkdownRewriteEngine, TwoPhaseBlockToken>( (e, t) => t.Extract(gfm.Parser))); tokens = rewriter.Rewrite(tokens); return(tokens); }