Skip to content

Commit 26dd0d6

Browse files
committed
support :language: on {literalinclude}
1 parent b409db3 commit 26dd0d6

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,18 @@ private void WriteLiteralIncludeBlock(HtmlRenderer renderer, IncludeBlock block)
231231
return;
232232

233233
var file = block.FileSystem.FileInfo.New(block.IncludePath);
234-
var html = block.FileSystem.File.ReadAllText(file.FullName);
235-
renderer.Write(html);
234+
var content = block.FileSystem.File.ReadAllText(file.FullName);
235+
if (string.IsNullOrEmpty(block.Language))
236+
renderer.Write(content);
237+
else
238+
{
239+
var slice = Code.Create(new CodeViewModel
240+
{
241+
CrossReferenceName = null, Language = block.Language, Caption = null
242+
});
243+
RenderRazorSlice(slice, renderer, content);
244+
}
245+
236246
}
237247

238248
private void WriteIncludeBlock(HtmlRenderer renderer, IncludeBlock block)
@@ -249,6 +259,15 @@ private void WriteIncludeBlock(HtmlRenderer renderer, IncludeBlock block)
249259
//RenderRazorSlice(slice, renderer, block);
250260
}
251261

262+
private static void RenderRazorSlice<T>(RazorSlice<T> slice, HtmlRenderer renderer, string contents)
263+
{
264+
var html = slice.RenderAsync().GetAwaiter().GetResult();
265+
var blocks = html.Split("[CONTENT]", 2, StringSplitOptions.RemoveEmptyEntries);
266+
renderer.Write(blocks[0]);
267+
renderer.Write(contents);
268+
renderer.Write(blocks[1]);
269+
}
270+
252271
private static void RenderRazorSlice<T>(RazorSlice<T> slice, HtmlRenderer renderer, DirectiveBlock obj)
253272
{
254273
var html = slice.RenderAsync().GetAwaiter().GetResult();

src/Elastic.Markdown/Myst/Directives/IncludeBlock.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public class IncludeBlock(DirectiveBlockParser parser, Dictionary<string, string
1717

1818
public string? IncludePath { get; private set; }
1919

20-
public bool Literal { get; private set; }
20+
public bool Literal { get; protected set; }
21+
22+
public string? Language { get; private set; }
2123

2224
public bool Found { get; private set; }
2325

@@ -26,22 +28,34 @@ public class IncludeBlock(DirectiveBlockParser parser, Dictionary<string, string
2628
public override void FinalizeAndValidate()
2729
{
2830
var includePath = Arguments; //todo validate
29-
Literal = bool.TryParse(Properties.GetValueOrDefault("literal"), out var b) && b;
31+
Literal |= bool.TryParse(Properties.GetValueOrDefault("literal"), out var b) && b;
32+
Language = Properties.GetValueOrDefault("language");
3033
if (includePath is null)
3134
{
3235
//TODO emit empty error
3336
}
3437
else
3538
{
36-
IncludePath = Path.Combine(IncludeFromPath.Directory!.FullName, includePath);
39+
var includeFrom = IncludeFromPath.Directory!.FullName;
40+
if (includePath.StartsWith('/'))
41+
includeFrom = DocumentationSourcePath.FullName;
42+
43+
IncludePath = Path.Combine(includeFrom, includePath.TrimStart('/'));
3744
if (FileSystem.File.Exists(IncludePath))
3845
Found = true;
46+
else
47+
{
48+
//TODO emit error
49+
}
3950
}
4051

4152

4253
}
4354
}
4455

4556

46-
public class LiteralIncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
47-
: IncludeBlock(parser, properties, context);
57+
public class LiteralIncludeBlock : IncludeBlock
58+
{
59+
public LiteralIncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
60+
: base(parser, properties, context) => Literal = true;
61+
}

0 commit comments

Comments
 (0)