Skip to content

Commit fbe69b9

Browse files
committed
Сделано расширение Markdig для включения дополнительных файлов Markdown.
1 parent 10e8296 commit fbe69b9

File tree

5 files changed

+150
-6
lines changed

5 files changed

+150
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Markdig.Helpers;
2+
using Markdig.Parsers;
3+
using Markdig.Syntax;
4+
5+
namespace Markus.MarkdigExtensions.AutoInclude
6+
{
7+
internal class AutoIncludeBlock : LeafBlock
8+
{
9+
public AutoIncludeBlock(BlockParser? parser) : base(parser)
10+
{
11+
ProcessInlines = false;
12+
}
13+
14+
public StringSlice Filename { get; init; }
15+
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using Markdig.Helpers;
2+
using Markdig.Parsers;
3+
using Markdig.Parsers.Inlines;
4+
using Markdig.Syntax.Inlines;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Markus.MarkdigExtensions.AutoInclude
12+
{
13+
internal class AutoIncludeParser : BlockParser
14+
{
15+
16+
private readonly string _openingSequence = "@@[[";
17+
private readonly string _closingSequence = "]]";
18+
19+
private readonly char _openingCharacter = '@';
20+
private readonly char _firstClosingCharacter = ']';
21+
22+
public AutoIncludeParser() {
23+
24+
OpeningCharacters = new[] { _openingCharacter };
25+
}
26+
27+
public override BlockState TryOpen(BlockProcessor processor)
28+
{
29+
if (processor.IsCodeIndent)
30+
{
31+
return BlockState.None;
32+
}
33+
34+
var sourceColumn = processor.Column;
35+
var line = processor.Line;
36+
var sourcePosition = line.Start;
37+
var c = processor.NextChar();
38+
39+
int index = 1;
40+
41+
42+
while(c != '\0' && index < _openingSequence.Length)
43+
{
44+
if (c != _openingSequence[index])
45+
{
46+
processor.Column = sourceColumn;
47+
processor.Line = line;
48+
return BlockState.None;
49+
}
50+
index++;
51+
c = processor.NextChar();
52+
}
53+
54+
//Exit if invalid opening sequence
55+
if (index != _openingSequence.Length)
56+
{
57+
processor.Column = sourceColumn;
58+
processor.Line = line;
59+
return BlockState.None;
60+
}
61+
62+
StringBuilder sb = new StringBuilder();
63+
64+
if (c != _firstClosingCharacter)
65+
{
66+
sb.Append(c);
67+
while (c != _firstClosingCharacter)
68+
{
69+
c = processor.NextChar();
70+
}
71+
}
72+
index = 0;
73+
74+
while(c != '\0' && index < _closingSequence.Length)
75+
{
76+
if (c != _closingSequence[index])
77+
{
78+
processor.Column = sourceColumn;
79+
processor.Line = line;
80+
return BlockState.None;
81+
}
82+
index++;
83+
c = processor.NextChar();
84+
}
85+
if (index != _closingSequence.Length)
86+
{
87+
processor.Column = sourceColumn;
88+
processor.Line = line;
89+
return BlockState.None;
90+
}
91+
92+
var autoIncludeBlock = new AutoIncludeBlock(this)
93+
{
94+
Column = sourceColumn,
95+
Span = { Start = sourcePosition },
96+
Filename = new StringSlice(sb.ToString())
97+
};
98+
processor.GoToColumn(sourceColumn + _openingSequence.Length + sb.Length + _closingSequence.Length + 1);
99+
processor.NewBlocks.Push(autoIncludeBlock);
100+
101+
autoIncludeBlock.Span.End = processor.Line.End;
102+
103+
return BlockState.Break;
104+
}
105+
}
106+
}

MarkdigExtensions/ExtensionUsings.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Markdig;
2+
using Markus.MarkdigExtensions.AutoInclude;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Markus.MarkdigExtensions
10+
{
11+
internal static class ExtensionUsings
12+
{
13+
public static MarkdownPipelineBuilder UseAutoInclude(this MarkdownPipelineBuilder builder)
14+
{
15+
if (!builder.BlockParsers.Contains<AutoIncludeParser>())
16+
{
17+
builder.BlockParsers.Insert(0, new AutoIncludeParser());
18+
}
19+
return builder;
20+
}
21+
22+
}
23+
}

Markus.csproj

-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
</Content>
4949
</ItemGroup>
5050

51-
<ItemGroup>
52-
<Folder Include="MarkdigExtensions\" />
53-
</ItemGroup>
54-
5551
<ItemGroup>
5652
<None Update="Schemas\manifestSchema.json">
5753
<CopyToOutputDirectory>Never</CopyToOutputDirectory>

Services/MarkdownService.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Markdig;
55
using Markdig.Syntax;
66
using Markdig.Syntax.Inlines;
7+
using Markus.MarkdigExtensions;
8+
using Markus.MarkdigExtensions.AutoInclude;
79
using Markus.Processing;
810
using MarkTables = Markdig.Extensions.Tables;
911

@@ -29,6 +31,7 @@ static MarkdownService()
2931
Pipeline = new MarkdownPipelineBuilder()
3032
.UseFootnotes()
3133
.UseAdvancedExtensions()
34+
.UseAutoInclude()
3235
.Build();
3336

3437
}
@@ -44,7 +47,7 @@ public static async Task<IEnumerable<MarkdownObject>> GetMarkdownTokens(string f
4447
{
4548

4649
//This functionality is not implemented yet: recursive include_once like behaivor of markdown parser
47-
if (recursive && processedPaths == null)
50+
if (recursive && processedPaths is null)
4851
{
4952
processedPaths = new HashSet<string>();
5053
}
@@ -64,7 +67,6 @@ public static void ProcessBlocks(IEnumerable<MarkdownObject> blocks, Wordprocess
6467

6568
foreach (MarkdownObject block in blocks)
6669
{
67-
6870
switch (block)
6971
{
7072

0 commit comments

Comments
 (0)