TSBSoftware.TextBlock
1.0.4
dotnet add package TSBSoftware.TextBlock --version 1.0.4
NuGet\Install-Package TSBSoftware.TextBlock -Version 1.0.4
<PackageReference Include="TSBSoftware.TextBlock" Version="1.0.4" />
<PackageVersion Include="TSBSoftware.TextBlock" Version="1.0.4" />
<PackageReference Include="TSBSoftware.TextBlock" />
paket add TSBSoftware.TextBlock --version 1.0.4
#r "nuget: TSBSoftware.TextBlock, 1.0.4"
#addin nuget:?package=TSBSoftware.TextBlock&version=1.0.4
#tool nuget:?package=TSBSoftware.TextBlock&version=1.0.4
TextBlock
Makes F# multiline strings work like C# Raw String Literals, or Java Text Blocks.
This project was inspired by an article related to Java Text Blocks. This functionality felt like a missing feature in the F# language, and I wanted to make it available to everyone.
Who is it for?
F# developers who would like to utilize multiline strings to format content, such as: html, xml, sql, markdown, templates, etc.
Features
TextBlock provides a robust set of tools to simplify working with multiline strings in F#:
- Automatic Indentation Stripping: Removes unwanted leading whitespace (spaces or tabs) from each line, based on the minimum indentation, ensuring clean output for formats like HTML, SQL, or Markdown.
- Newline Escaping: Supports backslash (
\
) at the end of a line to suppress newlines, allowing long content to be split across lines in source code without affecting the output. - Trailing Space Preservation: Preserves trailing spaces using a pipe (
|
) marker at the end of a line, ideal for maintaining intentional spacing in templates or formatted text. - Custom Indentation: Allows additional indentation with a user-defined character (default is a space) and count, enabling flexible formatting for nested or styled output.
- Embedded Newline Support: Correctly handles embedded newline characters (e.g., via
Environment.NewLine
), ensuring accurate rendering of dynamic content like multi-line SQL queries or email templates. - Cross-Platform Compatibility: Uses
Environment.NewLine
for line endings, ensuring consistent behavior across Windows (\r\n
) and other platforms (\n
).
Why Use TextBlock?
Feature | F# Multiline Strings | TextBlock | C# Raw String Literals | Java Text Blocks |
---|---|---|---|---|
Auto-indent stripping | No | Yes | Yes | Yes |
Newline escaping | No | Yes | Yes | Yes |
Trailing space control | No | Yes | Partial | Yes |
Getting Started
To start using TextBlock, install the NuGet package into your project file.
dotnet add package TSBSoftware.TextBlock
Open the TextBlock namespace to use the extension method.
open TextBlock
let myText =
"""
<div>
<p>Hello</p>
</div>
"""
.TextBlock()
This will produce the following string:
<div>
<p>Hello</p>
</div>
Without using TextBlock, there would be unwanted leading indentation because of how F# processes multiline strings:
<div>
<p>Hello</p>
</div>
Additional Examples
Strings can contain embedded newline characters.
let embeddedNewlines =
let nl = System.Environment.NewLine
$"""
This content has {nl}several lines
in {nl}the text.
"""
.TextBlock()
This will produce
This content has
several lines
in
the text.
Newlines can be escaped with a backslash.
let myText =
"""
Hello \
World!
"""
.TextBlock()
This removes the newline character and combines the text into a single line:
Hello World!
Spaces at the end of each line can be preserved.
let blockedLines =
"""
BlockLine |
Text |
"""
.TextBlock()
This will produce the following. Trailing spaces are shown as dots for clarity.
BlockLine���
Text��������
You can apply additional indentation with a specified level and an optional character (default is a space).
let someHtml =
"""
<div>
<p>Hello</p>
</div>
"""
.TextBlock(indent = 4, indentChar = '�')
This will produce the following:
����<div>
���� <p>Hello</p>
����</div>
Notes
Repeated Application: TextBlock is designed for one-shot processing of multiline strings. Applying it multiple times (e.g.,
myText.TextBlock().TextBlock()
) is not recommended, as it may alter indentation or formatting unexpectedly.Platform Newlines: Output uses
Environment.NewLine
, which may produce\r\n
on Windows or\n
on other platforms, ensuring compatibility with your target systems.
License
- License: MIT (see LICENSE for details)
Source Code
The source code and tests are available on GitHub.
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.