Skip to content

Commit

Permalink
Add explit name parameter (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
kokorins authored Jul 13, 2024
1 parent 38f81a6 commit 7b014c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
13 changes: 13 additions & 0 deletions Sources/DotNetGraph.Tests/Core/DotNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ public async Task CompileNodeWithColor()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("Test [\n\t\"color\"=\"#FF0000\"\n]\n");
}

[TestMethod]
public async Task CompileNodesProperties()
{
var node = new DotNode()
.WithIdentifier("node", quoteReservedWords: false).WithStyle(DotNodeStyle.Filled);
await using var writer = new StringWriter();
var context = new CompilationContext(writer, new CompilationOptions());
await node.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("node [\n\t\"style\"=\"filled\"\n]\n");
}
}
7 changes: 5 additions & 2 deletions Sources/DotNetGraph/Core/DotIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace DotNetGraph.Core
{
public class DotIdentifier : IDotElement, IEquatable<DotIdentifier>
{
private readonly bool _quoteReservedWords;

private static readonly Regex NoQuotesRequiredRegex
= new Regex("^([a-zA-Z\\200-\\377_][a-zA-Z\\200-\\3770-9_]*|[-]?(.[0-9]+|[0-9]+(.[0-9]+)?))$");

Expand All @@ -26,8 +28,9 @@ private static readonly Regex NoQuotesRequiredRegex

public bool IsHtml { get; set; } = false;

public DotIdentifier(string value, bool isHtml = false)
public DotIdentifier(string value, bool isHtml = false, bool quoteReservedWords = true)
{
_quoteReservedWords = quoteReservedWords;
Value = value;
IsHtml = isHtml;
}
Expand All @@ -41,7 +44,7 @@ public async Task CompileAsync(CompilationContext context)
}

var value = context.Options.AutomaticEscapedCharactersFormat ? Value.FormatGraphvizEscapedCharacters() : Value;
if (RequiresDoubleQuotes(value))
if (RequiresDoubleQuotes(value) && _quoteReservedWords)
await context.TextWriter.WriteAsync($"\"{value}\"");
else
await context.TextWriter.WriteAsync($"{value}");
Expand Down
5 changes: 3 additions & 2 deletions Sources/DotNetGraph/Extensions/DotNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ namespace DotNetGraph.Extensions
{
public static class DotNodeExtensions
{
public static DotNode WithIdentifier(this DotNode node, string identifier, bool isHtml = false)
public static DotNode WithIdentifier(this DotNode node, string identifier, bool isHtml = false,
bool quoteReservedWords = true)
{
node.Identifier = new DotIdentifier(identifier, isHtml);
node.Identifier = new DotIdentifier(identifier, isHtml, quoteReservedWords);
return node;
}

Expand Down

0 comments on commit 7b014c3

Please sign in to comment.