Skip to content

Commit 880b7b1

Browse files
authored
Add initial support for structured settings references (yaml) (#105)
1 parent 9290b34 commit 880b7b1

28 files changed

+1223
-105
lines changed

docs/source/elastic/reference/index.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
title: Automated Settings Reference
33
---
44

5-
Elastic docs v3 supports automatically generated configuration documentation OOTB.
6-
Simply supply a yaml spec and corresponding template file and content will be automatically built and updated.
75

8-
This section includes one example yaml file and two auto-generated outputs based on that file:
96

10-
* [example `yaml` file](source.md)
11-
* [generated output](generated.md)
7+
```{settings} kibana-alerting-action-settings.yml
8+
```

docs/source/elastic/reference/kibana-alerting-action-settings.yml

Lines changed: 750 additions & 0 deletions
Large diffs are not rendered by default.

docs/source/markup/substitutions.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
title: Substitutions
33
sub:
44
frontmatter_key: "Front Matter Value"
5+
a-key-with-dashes: "A key with dashes"
56
version: 7.17.0
67
---
78

89
Here are some variable substitutions:
910

10-
| Value | Source |
11-
| ------------------- | ------------ |
12-
| {{frontmatter_key}} | Front Matter |
11+
| Variable | Defined in |
12+
|-----------------------|--------------|
13+
| {{frontmatter_key}} | Front Matter |
14+
| {{a-key-with-dashes}} | Front Matter |
1315

1416
Substitutions should work in code blocks too.
1517

@@ -20,3 +22,6 @@ shasum -a 512 -c elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512 <1>
2022
tar -xzf elasticsearch-{{version}}-linux-x86_64.tar.gz
2123
cd elasticsearch-{{version}}/ <2>
2224
```
25+
26+
27+
Here is a variable with dashes: {{a-key-with-dashes}}

src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.IO.Abstractions;
66
using Elastic.Markdown.Myst;
7+
using Elastic.Markdown.Myst.Directives;
78
using Markdig.Helpers;
89
using Markdig.Parsers;
910

@@ -44,7 +45,7 @@ public static void EmitWarning(this InlineProcessor processor, int line, int col
4445
context.Build.Collector.Channel.Write(d);
4546
}
4647

47-
public static void EmitError(this ParserContext context, int line, int column, int length, string message)
48+
public static void EmitError(this ParserContext context, int line, int column, int length, string message, Exception? e = null)
4849
{
4950
if (context.SkipValidation) return;
5051
var d = new Diagnostic
@@ -53,7 +54,7 @@ public static void EmitError(this ParserContext context, int line, int column, i
5354
File = context.Path.FullName,
5455
Column = column,
5556
Line = line,
56-
Message = message,
57+
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
5758
Length = length
5859
};
5960
context.Build.Collector.Channel.Write(d);
@@ -74,13 +75,13 @@ public static void EmitWarning(this ParserContext context, int line, int column,
7475
context.Build.Collector.Channel.Write(d);
7576
}
7677

77-
public static void EmitError(this BuildContext context, IFileInfo file, string message)
78+
public static void EmitError(this BuildContext context, IFileInfo file, string message, Exception? e = null)
7879
{
7980
var d = new Diagnostic
8081
{
8182
Severity = Severity.Error,
8283
File = file.FullName,
83-
Message = message,
84+
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
8485
};
8586
context.Collector.Channel.Write(d);
8687
}
@@ -89,10 +90,42 @@ public static void EmitWarning(this BuildContext context, IFileInfo file, string
8990
{
9091
var d = new Diagnostic
9192
{
92-
Severity = Severity.Error,
93+
Severity = Severity.Warning,
9394
File = file.FullName,
9495
Message = message,
9596
};
9697
context.Collector.Channel.Write(d);
9798
}
99+
100+
public static void EmitError(this DirectiveBlock block, string message, Exception? e = null)
101+
{
102+
if (block.SkipValidation) return;
103+
104+
var d = new Diagnostic
105+
{
106+
Severity = Severity.Error,
107+
File = block.CurrentFile.FullName,
108+
Line = block.Line + 1,
109+
Column = block.Column,
110+
Length = block.Directive.Length + 5,
111+
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
112+
};
113+
block.Build.Collector.Channel.Write(d);
114+
}
115+
116+
public static void EmitWarning(this DirectiveBlock block, string message)
117+
{
118+
if (block.SkipValidation) return;
119+
120+
var d = new Diagnostic
121+
{
122+
Severity = Severity.Warning,
123+
File = block.CurrentFile.FullName,
124+
Line = block.Line + 1,
125+
Column = block.Column,
126+
Length = block.Directive.Length + 4,
127+
Message = message
128+
};
129+
block.Build.Collector.Channel.Write(d);
130+
}
98131
}

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private void ReadDocumentInstructions(MarkdownDocument document)
7575
if (document.FirstOrDefault() is YamlFrontMatterBlock yaml)
7676
{
7777
var raw = string.Join(Environment.NewLine, yaml.Lines.Lines);
78-
YamlFrontMatter = FrontMatterParser.Deserialize(raw);
78+
YamlFrontMatter = YamlSerialization.Deserialize<YamlFrontMatter>(raw);
7979
Title = YamlFrontMatter.Title;
8080
NavigationTitle = YamlFrontMatter.NavigationTitle;
8181
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
// See the LICENSE file in the project root for more information
44
namespace Elastic.Markdown.Myst.Directives;
55

6-
public class AdmonitionBlock(DirectiveBlockParser parser, string admonition, Dictionary<string, string> properties)
7-
: DirectiveBlock(parser, properties)
6+
public class AdmonitionBlock(
7+
DirectiveBlockParser parser,
8+
string admonition,
9+
Dictionary<string, string> properties,
10+
ParserContext context)
11+
: DirectiveBlock(parser, properties, context)
812
{
913
public string Admonition => admonition == "admonition" ? Classes?.Trim() ?? "note" : admonition;
1014

@@ -36,8 +40,8 @@ public override void FinalizeAndValidate(ParserContext context)
3640
}
3741

3842

39-
public class DropdownBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
40-
: AdmonitionBlock(parser, "admonition", properties)
43+
public class DropdownBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
44+
: AdmonitionBlock(parser, "admonition", properties, context)
4145
{
4246
// ReSharper disable once RedundantOverriddenMember
4347
public override void FinalizeAndValidate(ParserContext context)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Markdown.Diagnostics;
56
using Elastic.Markdown.Myst.FrontMatter;
67
using Markdig.Syntax;
78

89
namespace Elastic.Markdown.Myst.Directives;
910

10-
public class AppliesBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
11-
: DirectiveBlock(parser, properties)
11+
public class AppliesBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
12+
: DirectiveBlock(parser, properties, context)
1213
{
1314
public override string Directive => "mermaid";
1415

@@ -64,15 +65,15 @@ public override void FinalizeAndValidate(ParserContext context)
6465
}
6566

6667
if (Deployment is null)
67-
EmitError(context, "{applies} block with no product availability specified");
68+
this.EmitError("{applies} block with no product availability specified");
6869

6970
var index = Parent?.IndexOf(this);
7071
if (Parent is not null && index > 0)
7172
{
7273
var i = index - 1 ?? 0;
7374
var prevSib = Parent[i];
7475
if (prevSib is not HeadingBlock)
75-
EmitError(context, "{applies} should follow a heading");
76+
this.EmitError("{applies} should follow a heading");
7677
}
7778

7879
bool TryGetAvailability(string key, out ProductAvailability? semVersion)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
// See the LICENSE file in the project root for more information
44
namespace Elastic.Markdown.Myst.Directives;
55

6-
public class CodeBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties)
7-
: DirectiveBlock(parser, properties)
6+
public class CodeBlock(
7+
DirectiveBlockParser parser,
8+
string directive,
9+
Dictionary<string, string> properties,
10+
ParserContext context)
11+
: DirectiveBlock(parser, properties, context)
812
{
913
public override string Directive => directive;
1014
public string? Caption { get; private set; }

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file is licensed under the BSD-Clause 2 license.
66
// See the license.txt file in the project root for more information.
77

8+
using System.IO.Abstractions;
89
using Elastic.Markdown.Diagnostics;
910
using Markdig.Helpers;
1011
using Markdig.Syntax;
@@ -22,10 +23,22 @@ namespace Elastic.Markdown.Myst.Directives;
2223
/// <param name="parser">The parser used to create this block.</param>
2324
/// <param name="properties"></param>
2425
/// <param name="context"></param>
25-
public abstract class DirectiveBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
26+
public abstract class DirectiveBlock(
27+
DirectiveBlockParser parser,
28+
Dictionary<string, string> properties,
29+
ParserContext context
30+
)
2631
: ContainerBlock(parser), IFencedBlock
2732
{
28-
public IReadOnlyDictionary<string, string> Properties { get; } = properties;
33+
protected IReadOnlyDictionary<string, string> Properties { get; } = properties;
34+
35+
public BuildContext Build { get; } = context.Build;
36+
37+
public IFileInfo CurrentFile { get; } = context.Path;
38+
39+
public bool SkipValidation { get; } = context.SkipValidation;
40+
41+
public abstract string Directive { get; }
2942

3043
public string? CrossReferenceName { get; protected set; }
3144

@@ -91,13 +104,4 @@ protected bool PropBool(params string[] keys)
91104
return default;
92105
}
93106

94-
public abstract string Directive { get; }
95-
96-
protected void EmitError(ParserContext context, string message) =>
97-
context.EmitError(Line + 1, 1, Directive.Length + 4 , message);
98-
99-
protected void EmitWarning(ParserContext context, string message) =>
100-
context.EmitWarning(Line + 1, 1, Directive.Length + 4 , message);
101-
102-
103107
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
7474
throw new Exception("Expected parser context to be of type ParserContext");
7575

7676
if (info.IndexOf("{") == -1)
77-
return new CodeBlock(this, "raw", _admonitionData);
77+
return new CodeBlock(this, "raw", _admonitionData, context);
7878

7979
// TODO alternate lookup .NET 9
8080
var directive = info.ToString().Trim(['{', '}', '`']);
8181
if (_unsupportedBlocks.TryGetValue(directive, out var issueId))
82-
return new UnsupportedDirectiveBlock(this, directive, _admonitionData, issueId);
82+
return new UnsupportedDirectiveBlock(this, directive, _admonitionData, issueId, context);
8383

8484
if (info.IndexOf("{tab-set}") > 0)
85-
return new TabSetBlock(this, _admonitionData);
85+
return new TabSetBlock(this, _admonitionData, context);
8686

8787
if (info.IndexOf("{tab-item}") > 0)
88-
return new TabItemBlock(this, _admonitionData);
88+
return new TabItemBlock(this, _admonitionData, context);
8989

9090
if (info.IndexOf("{dropdown}") > 0)
91-
return new DropdownBlock(this, _admonitionData);
91+
return new DropdownBlock(this, _admonitionData, context);
9292

9393
if (info.IndexOf("{image}") > 0)
9494
return new ImageBlock(this, _admonitionData, context);
@@ -103,7 +103,7 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
103103
// leaving the parsing in until we are confident we don't want this
104104
// for dev-docs
105105
if (info.IndexOf("{mermaid}") > 0)
106-
return new MermaidBlock(this, _admonitionData);
106+
return new MermaidBlock(this, _admonitionData, context);
107107

108108
if (info.IndexOf("{include}") > 0)
109109
return new IncludeBlock(this, _admonitionData, context);
@@ -112,26 +112,29 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
112112
return new LiteralIncludeBlock(this, _admonitionData, context);
113113

114114
if (info.IndexOf("{applies}") > 0)
115-
return new AppliesBlock(this, _admonitionData);
115+
return new AppliesBlock(this, _admonitionData, context);
116+
117+
if (info.IndexOf("{settings}") > 0)
118+
return new SettingsBlock(this, _admonitionData, context);
116119

117120
foreach (var admonition in _admonitions)
118121
{
119122
if (info.IndexOf($"{{{admonition}}}") > 0)
120-
return new AdmonitionBlock(this, admonition, _admonitionData);
123+
return new AdmonitionBlock(this, admonition, _admonitionData, context);
121124
}
122125

123126
foreach (var version in _versionBlocks)
124127
{
125128
if (info.IndexOf($"{{{version}}}") > 0)
126-
return new VersionBlock(this, version, _admonitionData);
129+
return new VersionBlock(this, version, _admonitionData, context);
127130
}
128131

129132
foreach (var code in _codeBlocks)
130133
{
131134
if (info.IndexOf($"{{{code}}}") > 0)
132-
return new CodeBlock(this, code, _admonitionData);
135+
return new CodeBlock(this, code, _admonitionData, context);
133136
}
134-
return new UnknownDirectiveBlock(this, info.ToString(), _admonitionData);
137+
return new UnknownDirectiveBlock(this, info.ToString(), _admonitionData, context);
135138
}
136139

137140
public override bool Close(BlockProcessor processor, Block block)

0 commit comments

Comments
 (0)