Skip to content

Commit edde6e4

Browse files
authored
Enable FAL to read file in inclusion/codesnippet (dotnet#2415)
1 parent 237b354 commit edde6e4

File tree

9 files changed

+56
-30
lines changed

9 files changed

+56
-30
lines changed

src/Microsoft.DocAsCode.MarkdigEngine.Extensions/CodeSnippet/HtmlCodeSnippetRenderer.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Microsoft.DocAsCode.MarkdigEngine.Extensions
1313
using Markdig.Renderers;
1414
using Markdig.Renderers.Html;
1515
using Microsoft.DocAsCode.Common;
16+
using Microsoft.DocAsCode.Plugins;
1617

1718
public class HtmlCodeSnippetRenderer : HtmlObjectRenderer<CodeSnippet>
1819
{
@@ -236,16 +237,16 @@ public HtmlCodeSnippetRenderer(IMarkdownEngine engine, MarkdownContext context)
236237
protected override void Write(HtmlRenderer renderer, CodeSnippet codeSnippet)
237238
{
238239
var refFileRelativePath = ((RelativePath)codeSnippet.CodePath).BasedOn((RelativePath)_context.FilePath);
239-
var refPath = Path.Combine(_context.BasePath, refFileRelativePath.RemoveWorkingFolder());
240-
if (!File.Exists(refPath))
240+
241+
if (!EnvironmentContext.FileAbstractLayer.Exists(refFileRelativePath))
241242
{
242243
string tag = "ERROR CODESNIPPET";
243244
string message = $"Unable to find {refFileRelativePath}";
244245
ExtensionsHelper.GenerateNodeWithCommentWrapper(renderer, tag, message, codeSnippet.Raw, codeSnippet.Line);
245246
return;
246247
}
247-
248-
if(codeSnippet.DedentLength != null && codeSnippet.DedentLength < 0)
248+
249+
if (codeSnippet.DedentLength != null && codeSnippet.DedentLength < 0)
249250
{
250251
renderer.Write($"<!-- Dedent length {codeSnippet.DedentLength} should be positive. Auto-dedent will be applied. -->\n");
251252
}
@@ -262,24 +263,23 @@ private string GetContent(CodeSnippet obj)
262263
var currentFilePath = ((RelativePath)_context.FilePath).GetPathFromWorkingFolder();
263264
var refFileRelativePath = ((RelativePath)obj.CodePath).BasedOn(currentFilePath);
264265
_engine.ReportDependency(refFileRelativePath);
265-
266+
266267
var refPath = Path.Combine(_context.BasePath, refFileRelativePath.RemoveWorkingFolder());
267-
var allLines = File.ReadAllLines(refPath);
268+
var allLines = EnvironmentContext.FileAbstractLayer.ReadAllLines(refFileRelativePath);
268269

269270
// code range priority: tag > #L1 > start/end > range > default
270271
if (!string.IsNullOrEmpty(obj.TagName))
271272
{
272273
var lang = obj.Language ?? Path.GetExtension(refPath);
273-
List<CodeSnippetExtrator> extrators;
274-
if(!CodeLanguageExtractors.TryGetValue(lang, out extrators))
274+
if (!CodeLanguageExtractors.TryGetValue(lang, out List<CodeSnippetExtrator> extrators))
275275
{
276276
Logger.LogError($"{lang} is not supported languaging name, alias or extension for parsing code snippet with tag name, you can use line numbers instead");
277277
}
278278

279-
if(extrators != null)
279+
if (extrators != null)
280280
{
281281
var tagWithPrefix = tagPrefix + obj.TagName;
282-
foreach(var extrator in extrators)
282+
foreach (var extrator in extrators)
283283
{
284284
HashSet<int> tagLines = new HashSet<int>();
285285
var tagToCoderangeMapping = extrator.GetAllTags(allLines, ref tagLines);

src/Microsoft.DocAsCode.MarkdigEngine.Extensions/Inclusion/InclusionBlock/HtmlInclusionBlockRenderer.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Microsoft.DocAsCode.MarkdigEngine.Extensions
55
{
6-
using System.IO;
76
using System.Text.RegularExpressions;
87

98
using Markdig.Renderers;
@@ -47,8 +46,7 @@ protected override void Write(HtmlRenderer renderer, InclusionBlock inclusion)
4746
var currentFilePath = ((RelativePath)_context.FilePath).GetPathFromWorkingFolder();
4847
var includedFilePath = ((RelativePath)inclusion.Context.IncludedFilePath).BasedOn(currentFilePath);
4948

50-
var filePath = Path.Combine(_context.BasePath, includedFilePath.RemoveWorkingFolder());
51-
if (!File.Exists(filePath))
49+
if (!EnvironmentContext.FileAbstractLayer.Exists(includedFilePath))
5250
{
5351
Logger.LogWarning($"Can't find {includedFilePath}.");
5452
renderer.Write(inclusion.Context.GetRaw());
@@ -66,7 +64,7 @@ protected override void Write(HtmlRenderer renderer, InclusionBlock inclusion)
6664
return;
6765
}
6866

69-
var content = File.ReadAllText(filePath);
67+
var content = EnvironmentContext.FileAbstractLayer.ReadAllText(includedFilePath);
7068
var context = new MarkdownContextBuilder()
7169
.WithContext(_context)
7270
.WithFilePath(includedFilePath.RemoveWorkingFolder())

src/Microsoft.DocAsCode.MarkdigEngine.Extensions/Inclusion/InclusionInline/HtmlInclusionInlineRenderer.cs

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace Microsoft.DocAsCode.MarkdigEngine.Extensions
55
{
6-
using System.IO;
7-
86
using Markdig.Renderers;
97
using Markdig.Renderers.Html;
108
using Microsoft.DocAsCode.Common;
@@ -43,19 +41,18 @@ protected override void Write(HtmlRenderer renderer, InclusionInline inclusion)
4341
}
4442

4543
var currentFilePath = ((RelativePath)_context.FilePath).GetPathFromWorkingFolder();
46-
var includeFilePath = ((RelativePath)inclusion.Context.IncludedFilePath).BasedOn(currentFilePath);
44+
var includedFilePath = ((RelativePath)inclusion.Context.IncludedFilePath).BasedOn(currentFilePath);
4745

48-
var filePath = Path.Combine(_context.BasePath, includeFilePath.RemoveWorkingFolder());
49-
if (!File.Exists(filePath))
46+
if (!EnvironmentContext.FileAbstractLayer.Exists(includedFilePath))
5047
{
51-
Logger.LogWarning($"Can't find {includeFilePath}.");
48+
Logger.LogWarning($"Can't find {includedFilePath}.");
5249
renderer.Write(inclusion.Context.GetRaw());
5350

5451
return;
5552
}
5653

5754
var parents = _context.InclusionSet;
58-
if (parents != null && parents.Contains(includeFilePath))
55+
if (parents != null && parents.Contains(includedFilePath))
5956
{
6057
string tag = "ERROR INCLUDE";
6158
string message = $"Unable to resolve {inclusion.Context.GetRaw()}: Circular dependency found in \"{_context.FilePath}\"";
@@ -64,16 +61,16 @@ protected override void Write(HtmlRenderer renderer, InclusionInline inclusion)
6461
return;
6562
}
6663

67-
var content = File.ReadAllText(filePath);
64+
var content = EnvironmentContext.FileAbstractLayer.ReadAllText(includedFilePath);
6865
var context = new MarkdownContextBuilder()
6966
.WithContext(_context)
70-
.WithFilePath(includeFilePath.RemoveWorkingFolder())
67+
.WithFilePath(includedFilePath.RemoveWorkingFolder())
7168
.WithContent(content)
7269
.WithIsInline(true)
7370
.WithAddingIncludedFile(currentFilePath)
7471
.Build();
7572

76-
_engine.ReportDependency(includeFilePath);
73+
_engine.ReportDependency(includedFilePath);
7774
// Do not need to check if content is a single paragragh
7875
// context.IsInline = true will force it into a single paragragh and render with no <p></p>
7976
var result = _engine.Markup(context, _parameters);

test/Microsoft.DocAsCode.MarkdigEngine.Tests/CodeSnippetTest.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ namespace Microsoft.DocAsCode.MarkdigEngine.Tests
88
using System.Linq;
99

1010
using Microsoft.DocAsCode.Plugins;
11+
1112
using Xunit;
1213

13-
public class CodeSnippetTest
14+
[Collection("docfx STA")]
15+
public class CodeSnippetTest : TestBase
1416
{
1517
private static MarkupResult SimpleMarkup(string source)
1618
{

test/Microsoft.DocAsCode.MarkdigEngine.Tests/InclusionTest.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ namespace Microsoft.DocAsCode.MarkdigEngine.Tests
99
using System.Linq;
1010

1111
using Microsoft.DocAsCode.Plugins;
12+
1213
using Xunit;
1314

14-
public class InclusionTest
15+
[Collection("docfx STA")]
16+
public class InclusionTest : TestBase
1517
{
1618
[Fact]
1719
[Trait("Related", "Inclusion")]

test/Microsoft.DocAsCode.MarkdigEngine.Tests/InteractiveCodeTest.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace Microsoft.DocAsCode.MarkdigEngine.Tests
55
{
66
using Xunit;
77

8-
public class InteractiveCodeTest
8+
[Collection("docfx STA")]
9+
public class InteractiveCodeTest : TestBase
910
{
1011
[Fact]
1112
[Trait("Related", "InteractiveCode")]

test/Microsoft.DocAsCode.MarkdigEngine.Tests/LineNumberTest.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace Microsoft.DocAsCode.MarkdigEngine.Tests
1111
using Microsoft.DocAsCode.Plugins;
1212
using Xunit;
1313

14-
public class LineNumberTest
14+
[Collection("docfx STA")]
15+
public class LineNumberTest: TestBase
1516
{
1617
[Fact]
1718
[Trait("Related", "LineNumber")]

test/Microsoft.DocAsCode.MarkdigEngine.Tests/MarkdigServiceTest.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ namespace Microsoft.DocAsCode.MarkdigEngine.Tests
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
88

9-
using Markdig.Syntax;
109
using MarkdigEngine.Extensions;
10+
11+
using Markdig.Syntax;
1112
using Xunit;
1213

13-
public class MarkdigServiceTest
14+
[Collection("docfx STA")]
15+
public class MarkdigServiceTest : TestBase
1416
{
1517
[Fact]
1618
[Trait("Related", "MarkdigService")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.DocAsCode.MarkdigEngine.Tests
5+
{
6+
using System;
7+
8+
using Microsoft.DocAsCode.Common;
9+
using Microsoft.DocAsCode.Plugins;
10+
11+
public class TestBase : IDisposable
12+
{
13+
public TestBase()
14+
{
15+
EnvironmentContext.FileAbstractLayerImpl = FileAbstractLayerBuilder.Default.ReadFromRealFileSystem(".").WriteToRealFileSystem(".").Create();
16+
}
17+
18+
public void Dispose()
19+
{
20+
EnvironmentContext.Clean();
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)