示例#1
0
        public IMarkdownTokenRewriter Create()
        {
            var list = new List <IMarkdownTokenValidator>();

            foreach (var contract in ValidatorContracts)
            {
                foreach (IMarkdownTokenValidatorProvider vp in CompositionHost.GetExports(typeof(IMarkdownTokenValidatorProvider), contract))
                {
                    list.AddRange(vp.GetValidators());
                }
            }
            var context = new MarkdownRewriterContext(CompositionHost, TagValidators);

            list.Add(MarkdownTokenValidatorFactory.FromLambda <MarkdownTagInlineToken>(context.Validate));
            return(MarkdownTokenRewriterFactory.FromLambda(
                       (IMarkdownRewriteEngine engine, IMarkdownToken token) =>
            {
                using (new LoggerPhaseScope(MarkdownValidatePhaseName))
                {
                    foreach (var item in list)
                    {
                        item.Validate(token);
                    }
                }
                return null;
            }));
        }
示例#2
0
        public void TestGfmWithSequenceRewrite()
        {
            const string source   = @"
# A
## B
### C";
            const string expected = @"<h2 id=""a"">A</h2>
<h4 id=""b"">B</h4>
<h4 id=""c"">C</h4>
";

            var builder = new GfmEngineBuilder(new Options());

            builder.Rewriter =
                MarkdownTokenRewriterFactory.Sequence(
                    MarkdownTokenRewriterFactory.FromLambda(
                        (IMarkdownRewriteEngine e, MarkdownHeadingBlockToken t) =>
                        t.Depth <= 2 ? new MarkdownHeadingBlockToken(t.Rule, t.Context, t.Content, t.Id, t.Depth + 1, t.SourceInfo) : null),
                    MarkdownTokenRewriterFactory.FromLambda(
                        (IMarkdownRewriteEngine e, MarkdownHeadingBlockToken t) =>
                        t.Depth == 3 ? new MarkdownHeadingBlockToken(t.Rule, t.Context, t.Content, t.Id, t.Depth + 1, t.SourceInfo) : null)
                    );
            var engine = builder.CreateEngine(new HtmlRenderer());
            var result = engine.Markup(source);

            Assert.Equal(expected.Replace("\r\n", "\n"), result);
        }
示例#3
0
 protected void CreateRewriters()
 {
     Rewriter = MarkdownTokenRewriterFactory.Composite(
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureNoteBlockToken t) => new DfmNoteBlockToken(t.Rule, t.Context, t.NoteType.Substring("AZURE.".Length), t.Content, t.SourceInfo)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureBlockquoteBlockToken t) => new MarkdownBlockquoteBlockToken(t.Rule, t.Context, t.Tokens, t.SourceInfo)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, MarkdownLinkInlineToken t) => new MarkdownLinkInlineToken(t.Rule, t.Context, NormalizeAzureLink(t.Href, MarkdownExtension, t.Context, t.SourceInfo.Markdown), t.Title, t.Content, t.SourceInfo)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureSelectorBlockToken t) => new DfmSectionBlockToken(t.Rule, t.Context, GenerateAzureSelectorAttributes(t.SelectorType, t.SelectorConditions), t.SourceInfo)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureHtmlMetadataBlockToken t) => new DfmYamlHeaderBlockToken(t.Rule, t.Context, GenerateYamlHeaderContent(t.Properties, t.Tags), t.SourceInfo)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureMigrationIncludeBlockToken t) => new DfmIncludeBlockToken(t.Rule, t.Context, t.Src, t.Name, t.Title, t.SourceInfo.Markdown, t.SourceInfo)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureMigrationIncludeInlineToken t) => new DfmIncludeInlineToken(t.Rule, t.Context, t.Src, t.Name, t.Title, t.SourceInfo.Markdown, t.SourceInfo)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureVideoBlockToken t) => new DfmVideoBlockToken(t.Rule, t.Context, GenerateAzureVideoLink(t.Context, t.VideoId, t.SourceInfo.Markdown), t.SourceInfo)
             )
         );
 }
示例#4
0
 public DfmEngine CreateDfmEngine(object renderer)
 {
     return(new DfmEngine(
                CreateParseContext().SetBaseFolder(_baseDir ?? string.Empty).SetFallbackFolders(_fallbackFolders),
                MarkdownTokenRewriterFactory.Composite(
                    MarkdownTokenRewriterFactory.FromLambda(GetTabGroupIdRewriter()),
                    Rewriter),
                renderer,
                Options)
     {
         TokenTreeValidator = TokenTreeValidator,
         TokenAggregators = TokenAggregators,
     });
 }
示例#5
0
 protected void CreateRewriters()
 {
     Rewriter = MarkdownTokenRewriterFactory.Composite(
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureIncludeInlineToken t) => new DfmIncludeInlineToken(t.Rule, t.Context, t.Src, t.Name, t.Title, t.Raw, t.RawMarkdown)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureIncludeBlockToken t) => new DfmIncludeBlockToken(t.Rule, t.Context, t.Src, t.Name, t.Title, t.Raw, t.RawMarkdown)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureNoteBlockToken t) => new DfmNoteBlockToken(t.Rule, t.Context, t.NoteType.Substring("AZURE.".Length), t.Content, t.RawMarkdown)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureBlockquoteBlockToken t) => new DfmBlockquoteBlockToken(t.Rule, t.Context, t.Tokens, t.RawMarkdown)
             )
         );
 }
示例#6
0
 protected void CreateRewriters()
 {
     Rewriter = MarkdownTokenRewriterFactory.Composite(
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureNoteBlockToken t) => new DfmNoteBlockToken(t.Rule, t.Context, t.NoteType.Substring("AZURE.".Length), t.Content, t.RawMarkdown)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureBlockquoteBlockToken t) => new MarkdownBlockquoteBlockToken(t.Rule, t.Context, t.Tokens, t.RawMarkdown)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, MarkdownLinkInlineToken t) => new MarkdownLinkInlineToken(t.Rule, t.Context, AppendDefaultExtension(t.Href, MarkdownExtension), t.Title, t.Content, t.RawMarkdown)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureSelectorBlockToken t) => new DfmSectionBlockToken(t.Rule, t.Context, GenerateAzureSelectorAttributes(t.SelectorType, t.SelectorConditions), t.RawMarkdown)
             ),
         MarkdownTokenRewriterFactory.FromLambda(
             (IMarkdownRewriteEngine e, AzureHtmlMetadataBlockToken t) => new DfmYamlHeaderBlockToken(t.Rule, t.Context, GenerateYamlHeaderContent(t.Properties, t.Tags), t.RawMarkdown)
             )
         );
 }
示例#7
0
        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);
        }
示例#8
0
        public void ParseWithBadRewrite()
        {
            const string source = @"
# Heading
";

            var builder = new GfmEngineBuilder(new Options());

            builder.Rewriter =
                MarkdownTokenRewriterFactory.Loop(
                    MarkdownTokenRewriterFactory.Composite(
                        MarkdownTokenRewriterFactory.FromLambda(
                            (IMarkdownRewriteEngine e, MarkdownHeadingBlockToken t) => new MarkdownTextToken(t.Rule, t.Context, t.RawMarkdown, t.RawMarkdown)
                            ),
                        MarkdownTokenRewriterFactory.FromLambda(
                            (IMarkdownRewriteEngine e, MarkdownTextToken t) => new MarkdownHeadingBlockToken(t.Rule, t.Context, new InlineContent(ImmutableArray <IMarkdownToken> .Empty), "aaaa", 1, t.RawMarkdown)
                            )
                        ),
                    10);
            var engine = builder.CreateEngine(new HtmlRenderer());

            Assert.Throws <InvalidOperationException>(() => engine.Markup(source));
        }
示例#9
0
        public void TestSourceInfo_Table()
        {
            const string File   = "test.md";
            var          gfm    = new GfmEngineBuilder(new Options()).CreateEngine(new HtmlRenderer());
            var          tokens = gfm.Parser.Tokenize(
                SourceInfo.Create(@"
| H1 | H2 |
|----|----|
|R1C1|R1C2|
|R2C1|R2C2|
".Replace("\r\n", "\n"), File));
            var rewriter =
                new MarkdownRewriteEngine(
                    gfm,
                    MarkdownTokenRewriterFactory.FromLambda <IMarkdownRewriteEngine, TwoPhaseBlockToken>(
                        (e, t) => t.Extract(gfm.Parser)));

            tokens = rewriter.Rewrite(tokens);

            Assert.Equal(2, tokens.Length);
            Assert.IsType <MarkdownTableBlockToken>(tokens[1]);
            var table = (MarkdownTableBlockToken)tokens[1];

            Assert.Equal(2, table.Header.Length);
            Assert.Equal(2, table.Cells.Length);

            Assert.Equal(2, table.SourceInfo.LineNumber);
            Assert.Equal(File, table.SourceInfo.File);

            Assert.Equal(2, table.Header[0].SourceInfo.LineNumber);
            Assert.Equal(2, table.Header[1].SourceInfo.LineNumber);

            Assert.Equal(4, table.Cells[0][0].SourceInfo.LineNumber);
            Assert.Equal(4, table.Cells[0][1].SourceInfo.LineNumber);
            Assert.Equal(5, table.Cells[1][0].SourceInfo.LineNumber);
            Assert.Equal(5, table.Cells[1][1].SourceInfo.LineNumber);
        }
示例#10
0
        public void TestGfmWithRewrite()
        {
            const string source   = @"
Heading
=======
 
Sub-heading
-----------
  
### Another deeper heading
  
Paragraphs are separated
by a blank line.
 
Leave 2 spaces at the end of a line to do a  
line break
 
Text attributes *italic*, **bold**, 
`monospace`, ~~strikethrough~~ .
 
A [link](http://example.com).

Shopping list:
 
* apples
* oranges
* pears
 
Numbered list:
 
1. apples
2. oranges
3. pears
";
            const string expected = @"<p>Paragraphs are separated
by a blank line.</p>
<p>Leave 2 spaces at the end of a line to do a<br>line break</p>
<p>Text attributes <em>italic</em>, <strong>bold</strong>, 
<code>monospace</code>, <del>strikethrough</del> .</p>
<p>A <a href=""http://example.com"">link</a>.</p>
<p>Shopping list:</p>
<ul>
<li>apples</li>
<li>oranges</li>
<li>pears</li>
</ul>
<p>Numbered list:</p>
<ol>
<li>apples</li>
<li>oranges</li>
<li>pears</li>
</ol>
";

            var builder = new GfmEngineBuilder(new Options());

            builder.Rewriter =
                MarkdownTokenRewriterFactory.FromLambda(
                    (IMarkdownRewriteEngine e, MarkdownHeadingBlockToken t) => new MarkdownIgnoreToken(t.Rule, t.Context, t.RawMarkdown) // ignore all heading
                    );
            var engine = builder.CreateEngine(new HtmlRenderer());
            var result = engine.Markup(source);

            Assert.Equal(expected.Replace("\r\n", "\n"), result);
        }
示例#11
0
        public void TestSourceInfo_BlockquoteAndList()
        {
            const string File   = "test.md";
            var          gfm    = new GfmEngineBuilder(new Options()).CreateEngine(new HtmlRenderer());
            var          tokens = gfm.Parser.Tokenize(
                SourceInfo.Create(@"> blockquote
> [link text](sometarget)
> 
> - list item 1
>   - list item 1-1
>   - list item 1-2
> - list item 2
>
> more para.
".Replace("\r\n", "\n"), File));
            var rewriter =
                new MarkdownRewriteEngine(
                    gfm,
                    MarkdownTokenRewriterFactory.FromLambda <IMarkdownRewriteEngine, TwoPhaseBlockToken>(
                        (e, t) => t.Extract(gfm.Parser)));

            tokens = rewriter.Rewrite(tokens);

            Assert.Equal(1, tokens.Length);
            Assert.IsType <MarkdownBlockquoteBlockToken>(tokens[0]);
            var blockquote = (MarkdownBlockquoteBlockToken)tokens[0];

            Assert.Equal(3, blockquote.Tokens.Length);

            Assert.Equal(1, blockquote.SourceInfo.LineNumber);
            Assert.Equal(File, blockquote.SourceInfo.File);

            Assert.IsType <MarkdownParagraphBlockToken>(blockquote.Tokens[0]);
            {
                var para = (MarkdownParagraphBlockToken)blockquote.Tokens[0];

                Assert.Equal(1, para.SourceInfo.LineNumber);
                Assert.Equal(File, para.SourceInfo.File);

                Assert.Equal(2, para.InlineTokens.Tokens.Length);

                Assert.IsType <MarkdownTextToken>(para.InlineTokens.Tokens[0]);
                var text = (MarkdownTextToken)para.InlineTokens.Tokens[0];
                Assert.Equal(1, text.SourceInfo.LineNumber);
                Assert.Equal(File, text.SourceInfo.File);

                Assert.IsType <MarkdownLinkInlineToken>(para.InlineTokens.Tokens[1]);
                var link = (MarkdownLinkInlineToken)para.InlineTokens.Tokens[1];
                Assert.Equal(2, link.SourceInfo.LineNumber);
                Assert.Equal(File, link.SourceInfo.File);
            }
            Assert.IsType <MarkdownListBlockToken>(blockquote.Tokens[1]);
            {
                var list = (MarkdownListBlockToken)blockquote.Tokens[1];

                Assert.Equal(4, list.SourceInfo.LineNumber);
                Assert.Equal(File, list.SourceInfo.File);
                Assert.Equal(2, list.Tokens.Length);
                Assert.IsType <MarkdownListItemBlockToken>(list.Tokens[0]);
                {
                    var listItem = (MarkdownListItemBlockToken)list.Tokens[0];
                    Assert.Equal(4, listItem.SourceInfo.LineNumber);
                    Assert.Equal(File, listItem.SourceInfo.File);

                    Assert.IsType <MarkdownNonParagraphBlockToken>(listItem.Tokens[0]);
                    var np = (MarkdownNonParagraphBlockToken)listItem.Tokens[0];
                    Assert.Equal(1, np.Content.Tokens.Length);
                    Assert.Equal(4, np.SourceInfo.LineNumber);
                    Assert.Equal(File, np.SourceInfo.File);

                    Assert.IsType <MarkdownListBlockToken>(listItem.Tokens[1]);
                    var subList = (MarkdownListBlockToken)listItem.Tokens[1];
                    Assert.Equal(2, subList.Tokens.Length);
                    Assert.IsType <MarkdownListItemBlockToken>(subList.Tokens[0]);
                    {
                        var subListItem = (MarkdownListItemBlockToken)subList.Tokens[0];
                        Assert.Equal(5, subListItem.SourceInfo.LineNumber);
                        Assert.Equal(File, subListItem.SourceInfo.File);
                    }
                    Assert.IsType <MarkdownListItemBlockToken>(subList.Tokens[1]);
                    {
                        var subListItem = (MarkdownListItemBlockToken)subList.Tokens[1];
                        Assert.Equal(6, subListItem.SourceInfo.LineNumber);
                        Assert.Equal(File, subListItem.SourceInfo.File);
                    }
                }

                Assert.IsType <MarkdownListItemBlockToken>(list.Tokens[1]);
                {
                    var listItem = (MarkdownListItemBlockToken)list.Tokens[1];
                    Assert.Equal(7, listItem.SourceInfo.LineNumber);
                    Assert.Equal(File, listItem.SourceInfo.File);
                }
            }

            Assert.IsType <MarkdownParagraphBlockToken>(blockquote.Tokens[2]);
            {
                var para = (MarkdownParagraphBlockToken)blockquote.Tokens[2];
                Assert.Equal(9, para.SourceInfo.LineNumber);
                Assert.Equal(File, para.SourceInfo.File);
            }
        }
示例#12
0
        public void TestSourceInfo_Basic()
        {
            const string File   = "test.md";
            var          gfm    = new GfmEngineBuilder(new Options()).CreateEngine(new HtmlRenderer());
            var          tokens = gfm.Parser.Tokenize(
                SourceInfo.Create(@"

# HEAD1
First line.  
More line.
## HEAD2
Yeah!".Replace("\r\n", "\n"), File));
            var rewriter =
                new MarkdownRewriteEngine(
                    gfm,
                    MarkdownTokenRewriterFactory.FromLambda <IMarkdownRewriteEngine, TwoPhaseBlockToken>(
                        (e, t) => t.Extract(gfm.Parser)));

            tokens = rewriter.Rewrite(tokens);

            Assert.Equal(5, tokens.Length);
            Assert.IsType <MarkdownNewLineBlockToken>(tokens[0]);
            Assert.IsType <MarkdownHeadingBlockToken>(tokens[1]);
            Assert.IsType <MarkdownParagraphBlockToken>(tokens[2]);
            Assert.IsType <MarkdownHeadingBlockToken>(tokens[3]);
            Assert.IsType <MarkdownParagraphBlockToken>(tokens[4]);
            var para = (MarkdownParagraphBlockToken)tokens[2];

            Assert.Equal(3, para.InlineTokens.Tokens.Length);
            Assert.IsType <MarkdownTextToken>(para.InlineTokens.Tokens[0]);
            Assert.IsType <MarkdownBrInlineToken>(para.InlineTokens.Tokens[1]);
            Assert.IsType <MarkdownTextToken>(para.InlineTokens.Tokens[2]);

            Assert.Equal(1, tokens[0].SourceInfo.LineNumber);
            Assert.Equal(File, tokens[0].SourceInfo.File);
            Assert.Equal("\n\n", tokens[0].SourceInfo.Markdown);

            Assert.Equal(3, tokens[1].SourceInfo.LineNumber);
            Assert.Equal(File, tokens[1].SourceInfo.File);
            Assert.Equal("# HEAD1\n", tokens[1].SourceInfo.Markdown);

            Assert.Equal(4, tokens[2].SourceInfo.LineNumber);
            Assert.Equal(File, tokens[2].SourceInfo.File);
            Assert.Equal("First line.  \nMore line.\n", tokens[2].SourceInfo.Markdown);

            Assert.Equal(4, para.InlineTokens.Tokens[0].SourceInfo.LineNumber);
            Assert.Equal(File, para.InlineTokens.Tokens[0].SourceInfo.File);
            Assert.Equal("First line.", para.InlineTokens.Tokens[0].SourceInfo.Markdown);

            Assert.Equal(4, para.InlineTokens.Tokens[1].SourceInfo.LineNumber);
            Assert.Equal(File, para.InlineTokens.Tokens[1].SourceInfo.File);
            Assert.Equal("  \n", para.InlineTokens.Tokens[1].SourceInfo.Markdown);

            Assert.Equal(5, para.InlineTokens.Tokens[2].SourceInfo.LineNumber);
            Assert.Equal(File, para.InlineTokens.Tokens[2].SourceInfo.File);
            Assert.Equal("More line.", para.InlineTokens.Tokens[2].SourceInfo.Markdown);

            Assert.Equal(6, tokens[3].SourceInfo.LineNumber);
            Assert.Equal(File, tokens[3].SourceInfo.File);
            Assert.Equal("## HEAD2\n", tokens[3].SourceInfo.Markdown);

            Assert.Equal(7, tokens[4].SourceInfo.LineNumber);
            Assert.Equal(File, tokens[4].SourceInfo.File);
            Assert.Equal("Yeah!", tokens[4].SourceInfo.Markdown);
        }
示例#13
0
        public IMarkdownTokenRewriter Create()
        {
            var context = new MarkdownRewriterContext(CompositionHost, Validators);

            return(MarkdownTokenRewriterFactory.FromLambda <IMarkdownRewriteEngine, MarkdownTagInlineToken>(context.Validate));
        }