Skip to content
ConsoleMonster
Class

Markup

ConsoleMonster.Text

Namespace
ConsoleMonster.Text
Assembly
ConsoleMonster
Modifiers
static

Parses the Spectre.Console markup dialect: nested style tags ([bold red on blue], [cadetblue], [#ff8800], [link=https://…]), theme tokens ([pass], [muted]), [/] to close the innermost tag, and [[ / ]] for literal brackets.

Inheritance

  • object
  • Markup

Remarks

Lenient parsing (the default) never throws. A tag that is not a known style is removed and its text kept when a later [/] closes it (so [unknowncolour]text[/] shows text); an unknown tag that is never closed stays literal, because it is most likely text such as [x]. Set Markup.Strict (or pass strict: true) to throw a FormatException for anything Markup.Validate rejects instead.

Methods

public static string Escape(string? text) #

Escapes brackets so text is shown literally inside markup (Spectre's Markup.Escape).

Parameters

text string? required
Plain text, or null.

Returns

string Escaped text.
Interpolate 2 overloads
public static string Interpolate(FormattableString value) #

Builds markup from an interpolated string, escaping every argument (format specifiers apply first), so values never become tags: Markup.Interpolate($"[pass]{name}[/] took {elapsed:N1}s").

Parameters

value System.FormattableString required
The interpolated string.

Returns

string Markup with escaped arguments.
public static string Interpolate(IFormatProvider? provider, FormattableString value) #

Builds markup from an interpolated string with an explicit culture, escaping every argument.

Parameters

provider System.IFormatProvider? required
The culture for formatting the arguments.
value System.FormattableString required
The interpolated string.

Returns

string Markup with escaped arguments.
Parse 2 overloads
public static StyledText Parse(string? markup, ITheme? theme = null, ColorDepth depth = TrueColor) #

Parses markup; lenient unless Markup.Strict is set (see the remarks on Markup).

Parameters

markup string? required
Markup, or null for empty text.
theme ConsoleMonster.Styling.ITheme? = null optional
Theme token resolver; defaults to the built-in dark theme.
depth ConsoleMonster.Capabilities.ColorDepth = TrueColor optional
Target colour depth.

Returns

StyledText Styled lines, with tabs expanded to four spaces.

Exceptions

  • FormatException – The markup is invalid and Markup.Strict is set.
public static StyledText Parse(string? markup, ITheme? theme, ColorDepth depth, bool strict) #

Parses markup, leniently or strictly.

Parameters

markup string? required
Markup, or null for empty text.
theme ConsoleMonster.Styling.ITheme? required
Theme token resolver; defaults to the built-in dark theme.
depth ConsoleMonster.Capabilities.ColorDepth required
Target colour depth.
strict bool required
Whether invalid markup throws instead of being rendered leniently.

Returns

StyledText Styled lines, with tabs expanded to four spaces.

Exceptions

  • FormatException – The markup is invalid and strict is true.
public static string Strip(string? markup) #

Removes tags and resolves escapes (lenient: an unknown tag that is never closed stays literal).

Parameters

markup string? required
Markup, or null.

Returns

string Plain text with normalized newlines and expanded tabs.
public static bool TryParseStyle(string? specification, ITheme? theme, ColorDepth depth, out Style style) #

Parses a style, including decorations, foreground/background colours, links and theme tokens.

Parameters

specification string? required
Style words.
theme ConsoleMonster.Styling.ITheme? required
Token resolver; defaults to the built-in dark theme.
depth ConsoleMonster.Capabilities.ColorDepth required
Target colour depth.
style ConsoleMonster.Styling.Style required
Parsed style, or plain when invalid.

Returns

bool Whether the entire specification is valid.
Validate 2 overloads
public static bool Validate(string markup, out string? error) #

Strictly checks tags, escapes and nesting using built-in theme tokens.

Parameters

markup string required
Markup to validate.
error string? required
First error, or null when valid.

Returns

bool Whether all tags are recognized and balanced.
public static bool Validate(string markup, ITheme? theme, out string? error) #

Strictly checks markup with a custom theme token resolver.

Parameters

markup string required
Markup to validate.
theme ConsoleMonster.Styling.ITheme? required
Token resolver; defaults to the built-in dark theme.
error string? required
First error, or null when valid.

Returns

bool Whether all tags are recognized and balanced.

Properties

public static bool Strict { get; set; } #

When true, Markup.Parse and everything built on it (for example Terminal.WriteMarkupLine) throw a FormatException for invalid markup: unknown tags, unbalanced tags and unescaped brackets. Useful in tests and migration checks. Default false.

public static bool Shortcodes { get; set; } #

When true, :name: shortcodes in the text of markup become emoji (:white_check_mark: is ✅; see Shortcodes). Off by default, so colons in data are never touched. The emoji are written as they are on every glyph tier; the Emoji cell presenter turns them into Basic or ASCII equivalents.