forked from dotnet/docfx
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
872197c
commit 77439b6
Showing
7 changed files
with
175 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...gEngine.Extensions/Inclusion/InclusionBlockShortcut/HtmlInclusionShortcutBlockRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.DocAsCode.MarkdigEngine.Extensions | ||
{ | ||
using Markdig; | ||
using Markdig.Renderers; | ||
using Markdig.Renderers.Html; | ||
using System.Linq; | ||
|
||
public class HtmlInclusionShortcutBlocktRenderer : HtmlObjectRenderer<InclusionShorctutBlock> | ||
{ | ||
private readonly MarkdownContext _context; | ||
private MarkdownPipeline _pipeline; | ||
|
||
public HtmlInclusionShortcutBlocktRenderer(MarkdownContext context, MarkdownPipeline pipeline) | ||
{ | ||
_context = context; | ||
_pipeline = pipeline; | ||
} | ||
|
||
protected override void Write(HtmlRenderer renderer, InclusionShorctutBlock inclusion) | ||
{ | ||
var (content, includeFilePath) = _context.ReadFile(inclusion.IncludedFilePath, inclusion); | ||
|
||
if (content == null) | ||
{ | ||
_context.LogWarning("include-not-found", $"Invalid include link: '{inclusion.IncludedFilePath}'.", inclusion); | ||
renderer.Write(inclusion.GetRawToken()); | ||
return; | ||
} | ||
|
||
if (InclusionContext.IsCircularReference(includeFilePath, out var dependencyChain)) | ||
{ | ||
_context.LogWarning("circular-reference", $"Build has identified file(s) referencing each other: {string.Join(" --> ", dependencyChain.Select(file => $"'{file}'"))} --> '{includeFilePath}'", inclusion); | ||
renderer.Write(inclusion.GetRawToken()); | ||
return; | ||
} | ||
using (InclusionContext.PushInclusion(includeFilePath)) | ||
{ | ||
renderer.Write(Markdown.ToHtml(content, _pipeline)); | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...sCode.MarkdigEngine.Extensions/Inclusion/InclusionBlockShortcut/InclusionShorctutBlock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.DocAsCode.MarkdigEngine.Extensions | ||
{ | ||
using Markdig.Parsers; | ||
using Markdig.Syntax; | ||
|
||
public class InclusionShorctutBlock : ContainerBlock | ||
{ | ||
public string Identifier { get; set; } | ||
|
||
public string IncludedFilePath { get; set; } | ||
|
||
public object ResolvedFilePath { get; set; } | ||
|
||
public string GetRawToken() => $"@@{Identifier}"; | ||
|
||
public InclusionShorctutBlock(BlockParser parser): base(parser) | ||
{ | ||
|
||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...MarkdigEngine.Extensions/Inclusion/InclusionBlockShortcut/InclusionShortcutBlockParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.DocAsCode.MarkdigEngine.Extensions | ||
{ | ||
using Markdig.Helpers; | ||
using Markdig.Parsers; | ||
|
||
public class InclusionShortcutBlockParser : BlockParser | ||
{ | ||
private const string StartString = "@@"; | ||
|
||
public InclusionShortcutBlockParser() | ||
{ | ||
OpeningCharacters = new char[] { '@' }; | ||
} | ||
|
||
public override BlockState TryOpen(BlockProcessor processor) | ||
{ | ||
if (processor.IsCodeIndent) | ||
{ | ||
return BlockState.None; | ||
} | ||
|
||
// @@identifier | ||
var column = processor.Column; | ||
var line = processor.Line; | ||
var command = line.ToString(); | ||
|
||
if (!ExtensionsHelper.MatchStart(ref line, StartString, false)) | ||
{ | ||
return BlockState.None; | ||
} | ||
else | ||
{ | ||
if (line.CurrentChar == '+') | ||
{ | ||
line.NextChar(); | ||
} | ||
} | ||
|
||
string identifier = null; | ||
|
||
if (!ExtensionsHelper.MatchIdentifier(ref line, out identifier)) | ||
{ | ||
return BlockState.None; | ||
} | ||
|
||
while (line.CurrentChar.IsSpaceOrTab()) line.NextChar(); | ||
if (line.CurrentChar != '\0') | ||
{ | ||
return BlockState.None; | ||
} | ||
|
||
processor.NewBlocks.Push(new InclusionShorctutBlock(this) | ||
{ | ||
Identifier = identifier, | ||
IncludedFilePath = $"~/includes/{identifier}.md", | ||
Line = processor.LineIndex, | ||
Column = column, | ||
}); | ||
|
||
return BlockState.BreakDiscard; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters