Skip to content

Commit b69c8cc

Browse files
authored
Merge pull request #16 from crwsolutions/feature/add-toml-writer
Add TOML writer with syntax highlighting support
2 parents a1b4abc + c857119 commit b69c8cc

15 files changed

Lines changed: 482 additions & 8 deletions

File tree

NTokenizers.Extensions.Spectre.Console.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
<Project Path="tests/NTokenizers.Extensions.Spectre.Console.ShowCase.Typescript/NTokenizers.Extensions.Spectre.Console.ShowCase.Typescript.csproj" />
1414
<Project Path="tests/NTokenizers.Extensions.Spectre.Console.ShowCase.Xml/NTokenizers.Extensions.Spectre.Console.ShowCase.Xml.csproj" />
1515
<Project Path="tests/NTokenizers.Extensions.Spectre.Console.ShowCase.Yaml/NTokenizers.Extensions.Spectre.Console.ShowCase.Yaml.csproj" />
16+
<Project Path="tests/NTokenizers.Extensions.Spectre.Console.ShowCase.Toml/NTokenizers.Extensions.Spectre.Console.ShowCase.Toml.csproj" />
1617
</Solution>

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# NTokenizers.Extensions.Spectre.Console
2-
**Stream-capable** Spectre.Console rendering extensions for NTokenizers (XML, JSON, Markdown, Java-/TypeScript, CSS, HTML, C# and SQL), Style-rich console syntax highlighting
1+
# NTokenizers.Extensions.Spectre.Console
2+
**Stream-capable** Spectre.Console rendering extensions for NTokenizers (XML, JSON, Markdown, Java-/TypeScript, CSS, HTML, C#, SQL and TOML), Style-rich console syntax highlighting
33

44
This library builds on:
55
- **[Spectre.Console](https://spectreconsole.net/)** for advanced console rendering

docs/_config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ sidebar:
2626
url: "xml"
2727
- title: "Yaml"
2828
url: "yaml"
29+
- title: "Toml"
30+
url: "toml"
2931
- title: "AI Example"
3032
url: "ai"
3133

docs/toml.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
layout: default
3+
title: "Toml"
4+
---
5+
6+
# TOML Syntax Highlighting
7+
8+
The `AnsiConsoleTomlExtensions` class provides extension methods for writing TOML content to the console with syntax highlighting.
9+
10+
## Methods
11+
12+
### WriteTomlAsync (Stream, default styles)
13+
14+
Writes TOML content from a stream to the console with default styling asynchronously.
15+
16+
```csharp
17+
Task<string> WriteTomlAsync(this IAnsiConsole ansiConsole, Stream stream)
18+
```
19+
20+
**Parameters:**
21+
- `ansiConsole`: The console to write to.
22+
- `stream`: The stream containing TOML content.
23+
24+
**Returns:** The TOML content as a string.
25+
26+
### WriteTomlAsync (Stream, custom styles)
27+
28+
Writes TOML content from a stream to the console with custom styling asynchronously.
29+
30+
```csharp
31+
Task<string> WriteTomlAsync(this IAnsiConsole ansiConsole, Stream stream, TomlStyles tomlStyles)
32+
```
33+
34+
**Parameters:**
35+
- `ansiConsole`: The console to write to.
36+
- `stream`: The stream containing TOML content.
37+
- `tomlStyles`: The styles to use for TOML token coloring.
38+
39+
**Returns:** The TOML content as a string.
40+
41+
### WriteToml (Stream, default styles)
42+
43+
Writes TOML content from a stream to the console with default styling synchronously.
44+
45+
```csharp
46+
string WriteToml(this IAnsiConsole ansiConsole, Stream stream)
47+
```
48+
49+
**Parameters:**
50+
- `ansiConsole`: The console to write to.
51+
- `stream`: The stream containing TOML content.
52+
53+
**Returns:** The TOML content as a string.
54+
55+
### WriteToml (Stream, custom styles)
56+
57+
Writes TOML content from a stream to the console with custom styling synchronously.
58+
59+
```csharp
60+
string WriteToml(this IAnsiConsole ansiConsole, Stream stream, TomlStyles tomlStyles)
61+
```
62+
63+
**Parameters:**
64+
- `ansiConsole`: The console to write to.
65+
- `stream`: The stream containing TOML content.
66+
- `tomlStyles`: The styles to use for TOML token coloring.
67+
68+
**Returns:** The TOML content as a string.
69+
70+
### WriteToml (string, default styles)
71+
72+
Writes TOML content from a string to the console with default styling.
73+
74+
```csharp
75+
void WriteToml(this IAnsiConsole ansiConsole, string value)
76+
```
77+
78+
**Parameters:**
79+
- `ansiConsole`: The console to write to.
80+
- `value`: The TOML string to write.
81+
82+
### WriteToml (string, custom styles)
83+
84+
Writes TOML content from a string to the console with custom styling.
85+
86+
```csharp
87+
void WriteToml(this IAnsiConsole ansiConsole, string value, TomlStyles tomlStyles)
88+
```
89+
90+
**Parameters:**
91+
- `ansiConsole`: The console to write to.
92+
- `value`: The TOML string to write.
93+
- `tomlStyles`: The styles to use for TOML token coloring.
94+
95+
## Example Usage
96+
97+
### Basic Usage with String
98+
99+
```csharp
100+
using Spectre.Console;
101+
using NTokenizers.Extensions.Spectre.Console;
102+
103+
var tomlString = """
104+
# This is a TOML document
105+
106+
title = "TOML Example"
107+
108+
[owner]
109+
name = "Tom Preston-Werner"
110+
dob = 1979-05-27T07:32:00-08:00
111+
112+
[database]
113+
enabled = true
114+
ports = [ 8001, 8001, 8002 ]
115+
data = [ ["delta", "phi"], [3.14] ]
116+
temp_targets = { cpu = 79.5, case = 72.0 }
117+
""";
118+
119+
AnsiConsole.Console.WriteToml(tomlString);
120+
```
121+
122+
### Custom Styles
123+
124+
```csharp
125+
using Spectre.Console;
126+
using NTokenizers.Extensions.Spectre.Console;
127+
using NTokenizers.Extensions.Spectre.Console.Styles;
128+
129+
var customTomlStyles = TomlStyles.Default;
130+
customTomlStyles.Identifier = new Style(Color.Orange1);
131+
132+
AnsiConsole.Console.WriteToml(tomlString, customTomlStyles);
133+
```
134+
135+
### Async with Stream
136+
137+
```csharp
138+
using Spectre.Console;
139+
using NTokenizers.Extensions.Spectre.Console;
140+
141+
await AnsiConsole.Console.WriteTomlAsync(stream);
142+
```
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using NTokenizers.Toml;
2+
using NTokenizers.Extensions.Spectre.Console.Styles;
3+
using NTokenizers.Extensions.Spectre.Console.Writers;
4+
using System.Text;
5+
using Spectre.Console;
6+
7+
namespace NTokenizers.Extensions.Spectre.Console;
8+
9+
/// <summary>
10+
/// Provides extension methods for writing TOML content to the console with syntax highlighting.
11+
/// This class enables style-rich console output for TOML data using the Spectre.Console library
12+
/// and NTokenizers for tokenization.
13+
/// </summary>
14+
public static class AnsiConsoleTomlExtensions
15+
{
16+
/// <summary>
17+
/// Writes TOML content from a stream to the console with custom styling.
18+
/// </summary>
19+
/// <param name="ansiConsole">The console to write to.</param>
20+
/// <param name="stream">The stream containing TOML content.</param>
21+
/// <param name="tomlStyles">The styles to use for TOML token coloring.</param>
22+
/// <param name="encoding">The character encoding to use. If null, encoding will be detected from the stream's byte order mark (BOM).</param>
23+
/// <param name="ct">A cancellation token to observe while waiting for the task to complete.</param>
24+
/// <returns>The TOML content as a string.</returns>
25+
public static async Task<string> WriteTomlAsync(this IAnsiConsole ansiConsole, Stream stream, TomlStyles? tomlStyles = null, Encoding? encoding = null, CancellationToken ct = default)
26+
{
27+
var tomlWriter = new TomlWriter(ansiConsole, tomlStyles ?? TomlStyles.Default);
28+
if (encoding is null)
29+
{
30+
// Call overload without encoding to preserve BOM detection
31+
return await TomlTokenizer.Create().ParseAsync(
32+
stream,
33+
ct,
34+
tomlWriter.WriteToken
35+
);
36+
}
37+
else
38+
{
39+
// Call overload with encoding and cancellation token
40+
return await TomlTokenizer.Create().ParseAsync(
41+
stream,
42+
encoding,
43+
ct,
44+
tomlWriter.WriteToken
45+
);
46+
}
47+
}
48+
49+
/// <summary>
50+
/// Writes TOML content from a stream to the console with custom styling.
51+
/// </summary>
52+
/// <param name="ansiConsole">The console to write to.</param>
53+
/// <param name="stream">The stream containing TOML content.</param>
54+
/// <param name="tomlStyles">The styles to use for TOML token coloring.</param>
55+
/// <param name="encoding">The character encoding to use. If null, encoding will be detected from the stream's byte order mark (BOM).</param>
56+
/// <param name="ct">A cancellation token to observe while waiting for the task to complete.</param>
57+
/// <returns>The TOML content as a string.</returns>
58+
public static string WriteToml(this IAnsiConsole ansiConsole, Stream stream, TomlStyles? tomlStyles = null, Encoding? encoding = null, CancellationToken ct = default)
59+
{
60+
var t = Task.Run(() => WriteTomlAsync(ansiConsole, stream, tomlStyles, encoding, ct), ct);
61+
return t.GetAwaiter().GetResult();
62+
}
63+
64+
/// <summary>
65+
/// Writes TOML content from a string to the console with default styling.
66+
/// </summary>
67+
/// <param name="ansiConsole">The console to write to.</param>
68+
/// <param name="value">The TOML string to write.</param>
69+
public static void WriteToml(this IAnsiConsole ansiConsole, string value) =>
70+
WriteToml(ansiConsole, value, TomlStyles.Default);
71+
72+
/// <summary>
73+
/// Writes TOML content from a string to the console with custom styling.
74+
/// </summary>
75+
/// <param name="ansiConsole">The console to write to.</param>
76+
/// <param name="value">The TOML string to write.</param>
77+
/// <param name="tomlStyles">The styles to use for TOML token coloring.</param>
78+
public static void WriteToml(this IAnsiConsole ansiConsole, string value, TomlStyles tomlStyles)
79+
{
80+
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(value));
81+
var t = Task.Run(() => WriteTomlAsync(ansiConsole, stream, tomlStyles, Encoding.UTF8, default));
82+
t.GetAwaiter().GetResult();
83+
}
84+
}

src/NTokenizers.Extensions.Spectre.Console/NTokenizers.Extensions.Spectre.Console.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>latest</LangVersion>
@@ -13,9 +13,9 @@
1313
<Authors>crwsolutions</Authors>
1414
<Copyright>(c) crwsolutions</Copyright>
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
16-
<Description>Stream-capable Spectre.Console rendering extensions for NTokenizers (XML, JSON, Markdown, TypeScript, CSS, HTML, C# and SQL), Style-rich console syntax highlighting</Description>
17-
<PackageDescription>Stream-capable Spectre.Console rendering extensions for NTokenizers (XML, JSON, Markdown, TypeScript, CSS, HTML, C# and SQL), Style-rich console syntax highlighting</PackageDescription>
18-
<PackageTags>tokenizer;xml;json;md;markdown;typescript;css;html;c#;csharp;sql;stream;streaming;parse;parsing;dotnet;spectre.console;console;highlighting</PackageTags>
16+
<Description>Stream-capable Spectre.Console rendering extensions for NTokenizers (XML, JSON, Markdown, TypeScript, CSS, HTML, C#, SQL and TOML), Style-rich console syntax highlighting</Description>
17+
<PackageDescription>Stream-capable Spectre.Console rendering extensions for NTokenizers (XML, JSON, Markdown, TypeScript, CSS, HTML, C#, SQL and TOML), Style-rich console syntax highlighting</PackageDescription>
18+
<PackageTags>tokenizer;xml;json;md;markdown;typescript;css;html;c#;csharp;sql;toml;stream;streaming;parse;parsing;dotnet;spectre.console;console;highlighting</PackageTags>
1919
<RepositoryUrl>https://github.com/crwsolutions/NTokenizers.Extensions.Spectre.Console</RepositoryUrl>
2020
<RepositoryType>git</RepositoryType>
2121
<PackageReadmeFile>README.md</PackageReadmeFile>
@@ -26,7 +26,7 @@
2626
</PropertyGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="NTokenizers" Version="4.0.0" />
29+
<PackageReference Include="NTokenizers" Version="5.0.0" />
3030
<PackageReference Include="Spectre.Console" Version="0.54.0" />
3131
</ItemGroup>
3232

src/NTokenizers.Extensions.Spectre.Console/Styles/MarkdownStyles.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ public class MarkdownStyles
164164
/// </summary>
165165
public YamlStyles YamlStyles { get; } = YamlStyles.Default;
166166

167+
/// <summary>
168+
/// Gets the Toml styles used for rendering Toml content i markdown content.
169+
/// </summary>
170+
public TomlStyles TomlStyles { get; } = TomlStyles.Default;
171+
167172
/// <summary>
168173
/// Gets the XML styles used for rendering XML content in markdown content.
169174
/// </summary>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Spectre.Console;
2+
3+
namespace NTokenizers.Extensions.Spectre.Console.Styles;
4+
5+
/// <summary>
6+
/// Represents the styling configuration for TOML token rendering in Spectre.Console.
7+
/// This class defines the visual styles for various TOML tokens including identifiers,
8+
/// strings, numbers, booleans, date-times, and structural elements to enable style-rich console syntax highlighting.
9+
/// </summary>
10+
public sealed class TomlStyles
11+
{
12+
/// <summary>
13+
/// Gets the default TOML styles configuration.
14+
/// </summary>
15+
public static TomlStyles Default => new();
16+
17+
/// <summary>
18+
/// Gets or sets the style for whitespace tokens.
19+
/// </summary>
20+
public Style Whitespace { get; set; } = Style.Plain;
21+
22+
/// <summary>
23+
/// Gets or sets the style for comment tokens.
24+
/// </summary>
25+
public Style Comment { get; set; } = new Style(Color.Gray);
26+
27+
/// <summary>
28+
/// Gets or sets the style for identifier tokens (keys and table names).
29+
/// </summary>
30+
public Style Identifier { get; set; } = new Style(Color.DeepSkyBlue3_1);
31+
32+
/// <summary>
33+
/// Gets or sets the style for dot separators in dotted keys.
34+
/// </summary>
35+
public Style Dot { get; set; } = new Style(Color.Yellow);
36+
37+
/// <summary>
38+
/// Gets or sets the style for the equal sign (key-value separator).
39+
/// </summary>
40+
public Style Equal { get; set; } = new Style(Color.Yellow);
41+
42+
/// <summary>
43+
/// Gets or sets the style for comma separators.
44+
/// </summary>
45+
public Style Comma { get; set; } = new Style(Color.Yellow);
46+
47+
/// <summary>
48+
/// Gets or sets the style for open bracket tokens.
49+
/// </summary>
50+
public Style OpenBracket { get; set; } = new Style(Color.DeepSkyBlue4_1);
51+
52+
/// <summary>
53+
/// Gets or sets the style for close bracket tokens.
54+
/// </summary>
55+
public Style CloseBracket { get; set; } = new Style(Color.DeepSkyBlue4_1);
56+
57+
/// <summary>
58+
/// Gets or sets the style for open brace tokens.
59+
/// </summary>
60+
public Style OpenBrace { get; set; } = new Style(Color.DeepSkyBlue4_1);
61+
62+
/// <summary>
63+
/// Gets or sets the style for close brace tokens.
64+
/// </summary>
65+
public Style CloseBrace { get; set; } = new Style(Color.DeepSkyBlue4_1);
66+
67+
/// <summary>
68+
/// Gets or sets the style for string quote tokens.
69+
/// </summary>
70+
public Style StringQuote { get; set; } = new Style(Color.White);
71+
72+
/// <summary>
73+
/// Gets or sets the style for string value tokens.
74+
/// </summary>
75+
public Style StringValue { get; set; } = new Style(Color.White);
76+
77+
/// <summary>
78+
/// Gets or sets the style for numeric value tokens.
79+
/// </summary>
80+
public Style Number { get; set; } = new Style(Color.DarkSlateGray1);
81+
82+
/// <summary>
83+
/// Gets or sets the style for boolean value tokens.
84+
/// </summary>
85+
public Style Boolean { get; set; } = new Style(Color.Blue);
86+
87+
/// <summary>
88+
/// Gets or sets the style for date-time value tokens.
89+
/// </summary>
90+
public Style DateTime { get; set; } = new Style(Color.Magenta);
91+
}

0 commit comments

Comments
 (0)