Nest.Text
1.0.0
See the version list below for details.
dotnet add package Nest.Text --version 1.0.0
NuGet\Install-Package Nest.Text -Version 1.0.0
<PackageReference Include="Nest.Text" Version="1.0.0" />
<PackageVersion Include="Nest.Text" Version="1.0.0" />
<PackageReference Include="Nest.Text" />
paket add Nest.Text --version 1.0.0
#r "nuget: Nest.Text, 1.0.0"
#:package Nest.Text@1.0.0
#addin nuget:?package=Nest.Text&version=1.0.0
#tool nuget:?package=Nest.Text&version=1.0.0
Nest.Text
Nest.Text is a zero-dependency, fluent text generation library that helps you build structured content β from C#, Python, and YAML to HTML, XML, and more. It lets you describe what to generate β Nest takes care of how it's formatted.
π¦ Installation
dotnet add package Nest.Text
π What Is It?
Nest.Text provides a builder-style API to generate code, markup, or any structured text using:
.L(...)
β Write one or more lines.B(b => { })
β Begin a block with nested content.L(...).B(b => { })
β Write a logical block of lines
Nest adds or avoids line breaks based on chaining and structure awareness.
Line Break Behavior
Understanding how line breaks work with different usage patterns:
Usage Patterns
Lines
_.L("one");
_.L("two");
_.L("one");
_.L("two");
- β No line break will be added between these lines
Multiple Lines
β
Line break will be added here
_.L(
"one",
"two"
);
β
Line break will be added here
_.L("one");
_.L("two");
Multi-Line raw string literal
β
Line break will be added here
_.L(
"""
one
two
"""
);
β
Line break will be added here
_.L(
"""
one
two
"""
);
β
Line break will be added here
Strings having line breaks
β
Line break will be added here
_.L("one \n two");
_.L(); β οΈ First explicit line break will override implicit line break
_.L(); β
Explicit line break will be added here
_.L("one \n two");
β
Line break will be added here
Method Chaining
β
Line break will be added here
_.L("one")
.L("two").B(_ =>
{
_.L("three");
});
β
Line break will be added here
- β No line break between chained calls
βοΈ Options
_.Options.BlockStyle = BlockStyle.Braces; // or IndentOnly (Default)
_.Options.IndentSize = 4; // spaces per indent level
π Smart Quote Replacement
You can use backticks (`) instead of escaped quotes in your strings:
_.L("Console.WriteLine(`Hello World!`);");
// Outputs: Console.WriteLine("Hello World!");
To customize or disable:
_.Options.RegisterCharReplacement('`', '"');
_.Options.RemoveCharReplacement('`');
βοΈ Options (Live Behavior & Inheritance)
Options
in Nest.Text are live β changes apply immediately and affect all content added after the change.
π§ Live Indent Example
_.L("Console.WriteLine(`A`)"); // uses current indent size (e.g. 4)
_.Options.IndentSize = 0;
_.L("Console.WriteLine(`B`)"); // will have no indentation
_.Options.IndentSize = 4;
_.L("Console.WriteLine(`C`)"); // will again be indented
Each line reflects the indent size active at the time it's written.
π¦ BlockStyle Inheritance
When you change BlockStyle
, it affects the current block and all nested blocks by default:
_.L("namespace Demo").B(_ =>
{
_.Options.BlockStyle = BlockStyle.Braces;
_.L("class A").B(_ =>
{
_.L("void Print()").B(_ =>
{
_.L("print(`Hello`)");
});
});
});
β
namespace Demo
block itself & all blocks inside it will use now Braces
.
β Prevent Inheritance (Limit Scope)
If you want to apply a BlockStyle
to just the current block without affecting nested blocks, reset it at the end:
_.L("namespace Demo").B(_ =>
{
_.L("class A").B(_ =>
{
_.L("void Print()").B(_ =>
{
_.L("print(`Hello`)");
});
});
_.Options.BlockStyle = BlockStyle.Braces;
});
π Inherited Automatically
Any block you enter inherits the parentβs Options
by default β this includes indent size, block style, and character replacements. You can override or reset them at any level as needed.
This behavior makes it easy to dynamically shape output without needing multiple builders or deep config trees.
Reusing Common Builder Patterns
Since .B()
accepts a lambda, you can pass a method that takes an ITextBuilder
parameter β this lets you keep the structure clean by moving code blocks into reusable methods.
β Example: Extracting an If-Else Block
Instead of writing everything inline:
_.L("if (count > 6)").B(_ =>
{
_.L("Console.WriteLine(`Hello World!`);");
_.L("Console.WriteLine(`Hello Again!`);");
})
.L("else").B(_ =>
{
_.L("Console.WriteLine(`Hello World!`);");
});
You can extract it into a separate method:
void AddIfElse(ITextBuilder _)
{
_.L("if (count > 6)").B(_ =>
{
_.L("Console.WriteLine(`Hello World!`);");
_.L("Console.WriteLine(`Hello Again!`);");
})
.L("else").B(_ =>
{
_.L("Console.WriteLine(`Hello World!`);");
});
}
And call it like this:
_.L("public static void Main(string[] args)").B(_ => AddIfElse(_));
β
This keeps the Main
method block clean and readable.
π§© Using .Append()
.Append()
allows you to insert multiple external methods cleanly:
_.L("public static void Main(string[] args)").B(_ =>
_.Append(_ => AddIfElse(_)).Append(_ => AddLoop(_))
);
Each method AddIfElse()
, AddLoop()
still receives the same builder instance, so indentation and formatting stay consistent.
π Using .Chain()
If you're inside a chain, use .Chain()
and pass a method that takes IChainBuilder
:
void AddMainBody(IChainBuilder _)
{
_.B(_ =>
{
_.L("if (count > 6)").B(_ =>
{
_.L("Console.WriteLine(`Hello World!`);");
})
.L("else").B(_ =>
{
_.L("Console.WriteLine(`Goodbye!`);");
});
});
}
Then call it like:
_.L("public static void Main(string[] args)")
.Chain(_ => AddMainBody(_));
Why?
- β Keeps your generation logic modular.
- β Makes complex structures easier to read and maintain.
- β Allows mixing and matching reusable blocks.
This approach is especially helpful when generating large sections like methods, conditionals, loops, or class structures.
π§ͺ C# Example (Braces Block Style with Chaining)
var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.Braces;
_.Options.IndentSize = 4;
_.L("using System.Text;");
_.L("namespace MyProgram").B(_ =>
{
_.L("public class MyProgram").B(_ =>
{
_.L("public static void Main(string[] args)").B(_ =>
{
_.L("if (count > 6)").B(_ =>
{
_.L("Console.WriteLine(`Hello World!`);");
_.L("Console.WriteLine(`Hello Again!`);");
})
.L("else").B(_ =>
{
_.L("Console.WriteLine(`Hello World!`);");
});
});
});
});
Console.WriteLine(_.ToString());
π Python Example (IndentOnly)
var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;
_.L("def greet():").B(_ =>
{
_.L("print(`Hello World!`)");
_.L("print(`Hello Again!`)");
});
Console.WriteLine(_.ToString());
π HTML Example
var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;
_.Options.IndentSize = 2;
_.L("<div>").B(_ =>
{
_.L("<span>Hello World!</span>");
_.L("<span>Hello Again!</span>");
}
).L("</div>");
Console.WriteLine(_.ToString());
π XML Example
var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;
_.L("<config>").B(_ =>
{
_.L("<entry key=`theme`>dark</entry>");
_.L("<entry key=`lang`>en</entry>");
}
).L("</config>");
Console.WriteLine(_.ToString());
π YAML Example
var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;
_.L("library:").B(_ =>
{
_.L("name: `Nest`");
_.L("use: `Structured Text Generation`");
_.L("features:").B(_ =>
{
_.L("- Automated Indentation");
_.L("- Easy To Use");
_.L("- Zero Dependency");
});
});
Console.WriteLine(_.ToString());
πͺ Debugging
You can inspect the builder at any point during generation:
_.L("if (count > 0)").B(_ =>
{
_.L("Console.WriteLine(`Positive`);");
Console.WriteLine("--- Debug Snapshot ---");
Console.WriteLine(_.ToString()); // View current generated output
});
Alternatively, set a breakpoint anywhere and inspect the builder in your debugger. This is especially useful when checking structure, indentation, or formatting mid-flow.
Since everything is standard C#, you can step through and verify behavior interactively β no custom tooling needed.
π Summary
- Fluent and chainable API
- Smart formatting β line breaks where needed, not where not
- Custom indentation and block styles
- Backtick-friendly string writing
- Debuggable at every step
- No dependencies, works anywhere .NET runs
π Links
- π¦ NuGet: Nest.Text on NuGet
- π» GitHub: github.com/h-shahzaib/Nest.Text
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.