Skip to content

Commit fc36178

Browse files
committed
Rename to role
1 parent e06ce61 commit fc36178

File tree

3 files changed

+27
-114
lines changed

3 files changed

+27
-114
lines changed

docs/syntax/substitutions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ To use the variables in your files, surround them in curly brackets (`{{variable
3030

3131
- Regular text content
3232
- Code blocks (when `subs=true` is specified)
33-
- Inline code snippets (when `{subs=true}` prefix is used)
33+
- Inline code snippets (when `{subs}` prefix is used)
3434

3535
## Example
3636

@@ -177,10 +177,10 @@ cd elasticsearch-{{version}}/
177177

178178
## Inline code
179179

180-
Substitutions are also supported in inline code snippets using the `{subs=true}` syntax.
180+
Substitutions are also supported in inline code snippets using the `{subs}` syntax.
181181

182182
```markdown
183-
{subs=true}`wget elasticsearch-{{version.stack}}.tar.gz`
183+
{subs}`wget elasticsearch-{{version.stack}}.tar.gz`
184184
```
185185

186186
### Inline code examples
@@ -191,11 +191,11 @@ Substitutions are also supported in inline code snippets using the `{subs=true}`
191191

192192
Regular inline code: `wget elasticsearch-{{version.stack}}.tar.gz`
193193

194-
With substitutions: {subs=true}`wget elasticsearch-{{version.stack}}.tar.gz`
194+
With substitutions: {subs}`wget elasticsearch-{{version.stack}}.tar.gz`
195195

196-
Multiple variables: {subs=true}`export {{env-var}}={{version.stack}}`
196+
Multiple variables: {subs}`export {{env-var}}={{version.stack}}`
197197

198-
With mutations: {subs=true}`version {{version.stack | M.M}}`
198+
With mutations: {subs}`version {{version.stack | M.M}}`
199199

200200
:::
201201

src/Elastic.Markdown/Myst/InlineParsers/SubstitutionInlineCode/SubstitutionInlineCodeParser.cs

Lines changed: 10 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Elastic.Documentation.Diagnostics;
1111
using Elastic.Markdown.Diagnostics;
1212
using Elastic.Markdown.Myst.InlineParsers.Substitution;
13+
using Elastic.Markdown.Myst.Roles;
1314
using Markdig.Helpers;
1415
using Markdig.Parsers;
1516
using Markdig.Renderers;
@@ -20,7 +21,7 @@
2021
namespace Elastic.Markdown.Myst.InlineParsers.SubstitutionInlineCode;
2122

2223
[DebuggerDisplay("{GetType().Name} Line: {Line}, Content: {Content}, ProcessedContent: {ProcessedContent}")]
23-
public class SubstitutionInlineCodeLeaf(string content, string processedContent) : CodeInline(content)
24+
public class SubstitutionInlineCodeLeaf(string role, string content, string processedContent) : RoleLeaf(role, content)
2425
{
2526
public string ProcessedContent { get; } = processedContent;
2627
}
@@ -38,108 +39,18 @@ protected override void Write(HtmlRenderer renderer, SubstitutionInlineCodeLeaf
3839
}
3940
}
4041

41-
public partial class SubstitutionInlineCodeParser : InlineParser
42+
public partial class SubstitutionInlineCodeParser : RoleParser<SubstitutionInlineCodeLeaf>
4243
{
43-
public SubstitutionInlineCodeParser() => OpeningCharacters = ['{'];
44-
45-
private readonly SearchValues<char> _values = SearchValues.Create(['\r', '\n', ' ', '\t', '}']);
46-
private static readonly Regex SubstitutionPattern = SubstitutionRegex();
47-
48-
public override bool Match(InlineProcessor processor, ref StringSlice slice)
44+
protected override SubstitutionInlineCodeLeaf CreateRole(string role, string content, InlineProcessor parserContext)
4945
{
50-
var match = slice.CurrentChar;
51-
52-
if (processor.Context is not ParserContext context)
53-
return false;
54-
55-
Debug.Assert(match is not ('\r' or '\n'));
56-
57-
// Match the opened sticks
58-
var openSticks = slice.CountAndSkipChar(match);
59-
if (openSticks > 1)
60-
return false;
61-
62-
var span = slice.AsSpan();
63-
64-
var i = span.IndexOfAny(_values);
65-
66-
// We got to the end of the input before seeing the match character.
67-
if ((uint)i >= (uint)span.Length)
68-
return false;
69-
70-
var closeSticks = 0;
71-
while ((uint)i < (uint)span.Length && span[i] == '}')
72-
{
73-
closeSticks++;
74-
i++;
75-
}
76-
77-
if (closeSticks > 1)
78-
return false;
79-
80-
var roleContent = slice.AsSpan()[..i];
81-
82-
// Check if this matches the "subs=true" pattern
83-
if (!roleContent.SequenceEqual("{subs=true}".AsSpan()))
84-
return false;
85-
86-
// Check if the next character is a backtick
87-
if (i >= span.Length || span[i] != '`')
88-
return false;
89-
90-
var openingBacktickPos = i;
91-
var contentStartPos = i + 1; // Skip the opening backtick
92-
93-
var closingBacktickIndex = -1;
94-
for (var j = contentStartPos; j < span.Length; j++)
95-
{
96-
if (span[j] != '`')
97-
continue;
98-
closingBacktickIndex = j;
99-
break;
100-
}
101-
102-
if (closingBacktickIndex == -1)
103-
return false;
104-
105-
var contentSpan = span[openingBacktickPos..(closingBacktickIndex + 1)];
106-
107-
var startPosition = slice.Start;
108-
slice.Start = startPosition + roleContent.Length + contentSpan.Length;
109-
110-
// We've already skipped the opening sticks. Account for that here.
111-
startPosition -= openSticks;
112-
startPosition = Math.Max(startPosition, 0);
113-
114-
var start = processor.GetSourcePosition(startPosition, out var line, out var column);
115-
var end = processor.GetSourcePosition(slice.Start);
116-
var sourceSpan = new SourceSpan(start, end);
117-
118-
// Extract the actual code content (without backticks)
119-
var codeContent = contentSpan.Trim('`').ToString();
120-
121-
// Process substitutions in the code content
122-
var processedContent = ProcessSubstitutions(codeContent, context, processor, line, column);
46+
var context = (ParserContext)parserContext.Context!;
47+
var processedContent = ProcessSubstitutions(content, context, parserContext, 0, 0);
48+
return new SubstitutionInlineCodeLeaf(role, content, processedContent);
49+
}
12350

124-
var leaf = new SubstitutionInlineCodeLeaf(codeContent, processedContent)
125-
{
126-
Delimiter = '{',
127-
Span = sourceSpan,
128-
Line = line,
129-
Column = column,
130-
DelimiterCount = openSticks
131-
};
132-
133-
if (processor.TrackTrivia)
134-
{
135-
// startPosition and slice.Start include the opening/closing sticks.
136-
leaf.ContentWithTrivia =
137-
new StringSlice(slice.Text, startPosition + openSticks, slice.Start - openSticks - 1);
138-
}
51+
protected override bool Matches(ReadOnlySpan<char> role) => role.SequenceEqual("{subs}".AsSpan());
13952

140-
processor.Inline = leaf;
141-
return true;
142-
}
53+
private static readonly Regex SubstitutionPattern = SubstitutionRegex();
14354

14455
private static string ProcessSubstitutions(string content, ParserContext context, InlineProcessor processor, int line, int column)
14556
{

tests/Elastic.Markdown.Tests/Inline/SubstitutionInlineCodeTest.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ public class SubstitutionInlineCodeTest(ITestOutputHelper output) : InlineTest(o
1818
1919
Regular code: `wget elasticsearch-{{version}}.tar.gz`
2020
21-
Code with substitutions: {subs=true}`wget elasticsearch-{{version}}.tar.gz`
21+
Code with substitutions: {subs}`wget elasticsearch-{{version}}.tar.gz`
2222
23-
Multiple substitutions: {subs=true}`export {{env-var}}={{version}}`
23+
Multiple substitutions: {subs}`export {{env-var}}={{version}}`
2424
25-
With mutations: {subs=true}`version {{version | M.M}}`
25+
With mutations: {subs}`version {{version | M.M}}`
2626
"""
2727
)
2828
{
2929
[Fact]
30-
public void ProcessesSubstitutionsInInlineCode()
30+
public void TestSubstitutionInlineCode()
3131
{
32-
Html.Should()
33-
.Contain("<code>wget elasticsearch-{{version}}.tar.gz</code>") // Regular code should not process subs
34-
.And.Contain("<code>wget elasticsearch-8.15.0.tar.gz</code>") // Should process subs
35-
.And.Contain("<code>export MY_VAR=8.15.0</code>") // Multiple subs
36-
.And.Contain("<code>version 8.15</code>"); // Mutations
32+
// Check that regular code blocks are not processed
33+
Html.Should().Contain("<code>wget elasticsearch-{{version}}.tar.gz</code>");
34+
35+
// Check that {subs} inline code blocks have substitutions applied
36+
Html.Should().Contain("<code>wget elasticsearch-8.15.0.tar.gz</code>");
37+
Html.Should().Contain("<code>export MY_VAR=8.15.0</code>");
38+
Html.Should().Contain("<code>version 8.15</code>");
3739
}
3840

3941
[Fact]

0 commit comments

Comments
 (0)