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
529d58a
commit 872197c
Showing
7 changed files
with
143 additions
and
3 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
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
46 changes: 46 additions & 0 deletions
46
...MarkdigEngine.Extensions/Inclusion/InclusionInlineShortcut/HtmlInclusionInlineRenderer.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,46 @@ | ||
// 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 HtmlInclusionInlineShortcutRenderer : HtmlObjectRenderer<InclusionInlineShortcut> | ||
{ | ||
private readonly MarkdownContext _context; | ||
private readonly MarkdownPipeline _inlinePipeline; | ||
|
||
public HtmlInclusionInlineShortcutRenderer(MarkdownContext context, MarkdownPipeline inlinePipeline) | ||
{ | ||
_context = context; | ||
_inlinePipeline = inlinePipeline; | ||
} | ||
|
||
protected override void Write(HtmlRenderer renderer, InclusionInlineShortcut inclusion) | ||
{ | ||
var (content, includeFilePath) = _context.ReadFile(inclusion.IncludedFilePath, inclusion); | ||
|
||
if (content == null) | ||
{ | ||
_context.LogWarning("include-not-found", $"Cannot resolve '{inclusion.IncludedFilePath}' relative to '{InclusionContext.File}'.", 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, _inlinePipeline)); | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ode.MarkdigEngine.Extensions/Inclusion/InclusionInlineShortcut/InclusionInlineShortcut.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,18 @@ | ||
// 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.Syntax.Inlines; | ||
|
||
public class InclusionInlineShortcut : ContainerInline | ||
{ | ||
public string RawFilename { get; set; } | ||
|
||
public string IncludedFilePath { get; set; } | ||
|
||
public object ResolvedFilePath { get; set; } | ||
|
||
public string GetRawToken() => $"@@{RawFilename}"; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...rkdigEngine.Extensions/Inclusion/InclusionInlineShortcut/InclusionInlineShortcutParser.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,63 @@ | ||
// 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; | ||
using Markdig.Syntax; | ||
|
||
public class InclusionInlineShortcutParser : InlineParser | ||
{ | ||
private const string StartString = "@@"; | ||
|
||
public InclusionInlineShortcutParser() | ||
{ | ||
OpeningCharacters = new[] { '@' }; | ||
} | ||
|
||
public override bool Match(InlineProcessor processor, ref StringSlice slice) | ||
{ | ||
var startPosition = processor.GetSourcePosition(slice.Start, out var line, out var column); | ||
|
||
if (!ExtensionsHelper.MatchStart(ref slice, StartString, false)) | ||
{ | ||
return false; | ||
} | ||
|
||
var str = StringBuilderCache.Local(); | ||
|
||
str.Append(slice.CurrentChar); | ||
|
||
var c = slice.NextChar(); | ||
while (c.IsAlphaNumeric()) | ||
{ | ||
str.Append(c); | ||
c = slice.NextChar(); | ||
} | ||
|
||
if (str.Length == 0) | ||
{ | ||
return false; | ||
} | ||
|
||
string symbol = str.ToString(); | ||
if (string.IsNullOrWhiteSpace(symbol)) | ||
{ | ||
return false; | ||
} | ||
|
||
processor.Inline = new InclusionInlineShortcut | ||
{ | ||
RawFilename = symbol, | ||
IncludedFilePath = $"~/includes/{symbol}.md", | ||
Line = line, | ||
Column = column, | ||
Span = new SourceSpan(startPosition, processor.GetSourcePosition(slice.Start - 1)), | ||
IsClosed = true, | ||
}; | ||
|
||
return true; | ||
} | ||
} | ||
} |