Format multiline strings in Elm, inspired by C# Raw String Literals and Java Text Blocks.
This library simplifies working with multiline strings in Elm, removing unwanted indentation and providing flexible formatting options. It’s ideal for generating clean SQL queries, email templates, HTML, Markdown, or other structured text.
Elm developers who need to format multiline strings without the extra indentation that Elm’s triple-quoted strings (""") preserve by default.
- Automatic Indentation Stripping: Removes unwanted leading whitespace (spaces or tabs) from each line, based on the minimum indentation, for clean output.
- Newline Escaping: Supports backslash (
\) at the end of a line to suppress newlines, merging lines in the output. - Trailing Space Preservation: Preserves trailing spaces using a pipe (
|) marker, perfect for templates requiring precise spacing. - Custom Indentation: Adds user-defined indentation with a specified character (default is a space) and count for flexible formatting.
- String Interpolation: Supports placeholder replacement (e.g.,
{{key}}) for dynamic templates, ideal for email or HTML generation. - Configurable Newlines: Allows custom newline characters (default is
\n) for compatibility with different platforms or formats.
Add the package to your Elm project using:
elm install TSBSoftware/textblockUse the textBlock function to process a multiline string with default settings:
import TextBlock
myText =
textBlock
"""
<div>
<p>Hello</p>
</div>
"""
-- Output: "<div>\n <p>Hello</p>\n</div>"Without textBlock, Elm preserves leading indentation:
"""
<div>
<p>Hello</p>
</div>
"""
-- Output: "\n <div>\n <p>Hello</p>\n </div>\n"Apply additional indentation with a custom character:
import TextBlock
someHtml =
TextBlock.textBlockWith { defaultOptions | indent = 4, indentChar = '.' }
"""
<div>
<p>Hello</p>
</div>
"""
-- Output: "....<div>\n.... <p>Hello</p>\n....</div>"Replace placeholders in a template:
import TextBlock
greeting =
TextBlock.textBlockWithFormat defaultOptions
[ ( "name", "World" ) ]
"""
Hello {{name}}!
"""
-- Output: "Hello World!"Merge lines using a backslash:
import TextBlock
text =
textBlock
"""
Hello \
World!
"""
-- Output: "Hello World!"Keep trailing spaces with a pipe (|):
import TextBlock
blocked =
textBlock
"""
Blocked |
Text |
"""
-- Output: "Blocked \nText " (trailing spaces preserved)- One-Shot Usage:
textBlockis designed for single application. Applying it multiple times (e.g.,textBlock (textBlock value)) may alter indentation unexpectedly and is not recommended. - Newline Handling: The default newline is
\n. Customize it via TextBlockOptions if needed (e.g.,\r\nfor Windows compatibility).
- License: MIT (see LICENSE for details)