Skip to content

Commit

Permalink
block inline shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
dilyanrusev committed Sep 14, 2020
1 parent 872197c commit 77439b6
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ public static bool MatchStart(ref StringSlice slice, string startString, bool is
return index == startString.Length;
}

public static bool MatchIdentifier(ref StringSlice slice, out string identifier)
{
identifier = null;
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;
}

identifier = str.ToString();
if (string.IsNullOrWhiteSpace(identifier))
{
return false;
}

return true;
}

public static void ResetLineIndent(BlockProcessor processor)
{
processor.GoToColumn(processor.ColumnBeforeIndent);
Expand Down
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));
}
}
}
}
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)
{

}
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public void Setup(MarkdownPipelineBuilder pipeline)
var inlineShortcut = new InclusionInlineShortcutParser();
pipeline.InlineParsers.InsertBefore<XrefInlineShortParser>(inlineShortcut);
pipeline.InlineParsers.AddIfNotAlready(inlineShortcut);

pipeline.BlockParsers.AddIfNotAlready<InclusionShortcutBlockParser>();
}

public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
Expand All @@ -53,6 +55,11 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
htmlRenderer.ObjectRenderers.Insert(0, new HtmlInclusionBlockRenderer(_context, pipeline));
}

if (!htmlRenderer.ObjectRenderers.Contains<HtmlInclusionShortcutBlocktRenderer>())
{
htmlRenderer.ObjectRenderers.Insert(0, new HtmlInclusionShortcutBlocktRenderer(_context, pipeline));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace Microsoft.DocAsCode.MarkdigEngine.Extensions

public class InclusionInlineShortcut : ContainerInline
{
public string RawFilename { get; set; }
public string Identifier { get; set; }

public string IncludedFilePath { get; set; }

public object ResolvedFilePath { get; set; }

public string GetRawToken() => $"@@{RawFilename}";
public string GetRawToken() => $"@@{Identifier}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,15 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
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))
if (!ExtensionsHelper.MatchIdentifier(ref slice, out string identifier))
{
return false;
}

processor.Inline = new InclusionInlineShortcut
{
RawFilename = symbol,
IncludedFilePath = $"~/includes/{symbol}.md",
Identifier = identifier,
IncludedFilePath = $"~/includes/{identifier}.md",
Line = line,
Column = column,
Span = new SourceSpan(startPosition, processor.GetSourcePosition(slice.Start - 1)),
Expand Down

0 comments on commit 77439b6

Please sign in to comment.