Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- `CommandMenuMarkup` is written in MXP and Pueblo. MXP gets one `<SEND HREF="a|b" HINT="hint|A|B">` around
the whole text, so Mudlet and MUSHclient offer every choice on a right-click; Pueblo, which has no menus, gets
a link to the first command. A choice MXP cannot separate (a `|` or a control character in its command) is
left out, and a hint holding one gives way to the first choice's label. Both formats wrote only the text before.

### Fixed

- A heavy rule, and the line between several titles on a heavy rule or box edge, is drawn 2px thick in HTML, as a
Expand Down
50 changes: 49 additions & 1 deletion MarkupString.Mxp/Emitters/ElementMxpEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class ElementMxpEmitter(Type markupType, Func<string, bool>? sup
[
typeof(SoundMarkup), typeof(SoundStopMarkup), typeof(ImageMarkup), typeof(PaneMarkup),
typeof(ExpireLinksMarkup), typeof(RelocateMarkup), typeof(LoginPromptMarkup),
typeof(VariableMarkup), typeof(GaugeMarkup), typeof(StatusMarkup),
typeof(VariableMarkup), typeof(GaugeMarkup), typeof(StatusMarkup), typeof(CommandMenuMarkup),
];

public Type MarkupType { get; } = markupType;
Expand Down Expand Up @@ -172,12 +172,60 @@ public void Emit(IMarkup markup, ReadOnlySpan<char> body, in EmitContext context
.Close();
return;

case CommandMenuMarkup menu:
WriteMenu(menu, body, context, output);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return;

default:
output.Write(body);
return;
}
}

/// <summary>
/// A menu as one <c>&lt;SEND&gt;</c>: a click sends the first command, and a client with menus offers every
/// choice by its label (the hint first, then one label a command). MXP separates the commands with <c>|</c>,
/// so a choice whose command holds one, or a control character, is left out; with none left, only the text.
/// </summary>
private static void WriteMenu(CommandMenuMarkup menu, ReadOnlySpan<char> body, in EmitContext context, IBufferWriter<char> output)
{
var choices = menu.Choices.Where(choice => choice.Command.Length > 0 && !HasSeparatorOrControl(choice.Command)).ToArray();
if (choices.Length == 0)
{
output.Write(body);
return;
}

// One link around the whole text, however many runs an inner layer splits it into.
if (context.StartsRegion(menu))
{
// A hint holding the separator would read as one more label, so it gives way to the first choice's.
var head = menu.Hint is { } given && !HasSeparatorOrControl(given) ? given : Label(choices[0]);
var hint = choices.Length == 1
? head
: string.Join("|", [head, .. choices.Select(Label)]);
new MxpTag(output).Open("SEND")
.Named("HREF", string.Join("|", choices.Select(choice => choice.Command)))
.Named("HINT", hint)
.Flag("PROMPT", menu.Prompt)
.Close();
}

output.Write(body);
if (context.EndsRegion(menu)) output.Write("</SEND>");
}

/// <summary>A choice's label for the menu; a label holding the separator gives way to the command.</summary>
private static string Label(CommandChoice choice) =>
choice.Label.Length > 0 && !HasSeparatorOrControl(choice.Label) ? choice.Label : choice.Command;

private static bool HasSeparatorOrControl(string text)
{
foreach (var c in text)
if (c == '|' || char.IsControl(c)) return true;
return false;
}

private bool Supports(string element) => supports?.Invoke(element) ?? true;

private static string? Pixels(int? pixels) => pixels?.ToString(CultureInfo.InvariantCulture);
Expand Down
20 changes: 19 additions & 1 deletion MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal sealed class ElementPuebloEmitter(Type markupType) : IMarkupEmitter
internal static readonly Type[] Types =
[
typeof(SoundMarkup), typeof(SoundStopMarkup), typeof(ClearScreenMarkup), typeof(PrefetchMarkup),
typeof(ImageMarkup), typeof(PaneMarkup),
typeof(ImageMarkup), typeof(PaneMarkup), typeof(CommandMenuMarkup),
];

/// <summary>The pane name Pueblo reads as "back to wherever text was going before".</summary>
Expand Down Expand Up @@ -98,6 +98,24 @@ public void Emit(IMarkup markup, ReadOnlySpan<char> body, in EmitContext context
if (context.EndsRegion(markup)) Tag(output, "xch_pane", ("action", "redirect"), ("name", PreviousPane));
return;

case CommandMenuMarkup menu:
// Pueblo has no menus: the text becomes a link to the first command, as cmdlink() writes one.
if (menu.Choices.FirstOrDefault(choice => choice.Command.Length > 0 && !choice.Command.Any(char.IsControl)) is not { } first)
{
output.Write(body);
return;
}

if (context.StartsRegion(markup))
{
Tag(output, "a", ("xch_cmd", first.Command), ("xch_hint", menu.Hint ?? (first.Label.Length > 0 ? first.Label : null)),
("xch_mode", menu.Prompt ? "prompt" : null));
}

output.Write(body);
if (context.EndsRegion(markup)) output.Write("</a>");
return;

default:
output.Write(body);
return;
Expand Down
65 changes: 65 additions & 0 deletions MarkupString.Tests/SharedVocabularyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,71 @@ await Assert.That(Render(gauge, MarkupFormat.Html)).IsEqualTo(
}
}

[Test]
public async Task AMenuIsOneSendInMxpAndItsFirstCommandInPueblo()
{
var menu = MarkupText.Wrap(new CommandMenuMarkup([new("Look", "look Wren"), new("Page", "page Wren=")], "Wren"),
MarkupText.Plain("Wren"));

await Assert.That(Render(menu, MarkupFormat.Mxp)).IsEqualTo(
"<SEND HREF=\"look Wren|page Wren=\" HINT=Wren|Look|Page>Wren</SEND>");
await Assert.That(Render(menu, MarkupFormat.Pueblo)).IsEqualTo(
"<a xch_cmd=\"look Wren\" xch_hint=\"Wren\">Wren</a>");
await Assert.That(Render(menu, MarkupFormat.Ansi)).IsEqualTo("Wren");
await Assert.That(Render(menu, MarkupFormat.Plain)).IsEqualTo("Wren");
}

[Test]
public async Task AMenuOverSeveralRunsIsOneLink()
{
var name = MarkupText.Concat([
MarkupText.Wrap(AnsiMarkup.Create(foreground: new AnsiColor.Rgb(255, 0, 0)), MarkupText.Plain("Wr")),
MarkupText.Plain("en")]);
var menu = MarkupText.Wrap(new CommandMenuMarkup([new("Look", "look Wren"), new("Page", "page Wren=")]), name);

var mxp = Render(menu, MarkupFormat.Mxp);
var pueblo = Render(menu, MarkupFormat.Pueblo);

await Assert.That(mxp.Split("<SEND").Length).IsEqualTo(2);
await Assert.That(mxp).EndsWith("en</SEND>");
await Assert.That(pueblo.Split("<a ").Length).IsEqualTo(2);
await Assert.That(pueblo).EndsWith("en</a>");
}

[Test]
public async Task AMenuOfOneChoiceHintsWithItsLabelAndAPromptIsMarked()
{
var menu = MarkupText.Wrap(new CommandMenuMarkup([new("Say something", "say ")], Prompt: true), MarkupText.Plain("talk"));

await Assert.That(Render(menu, MarkupFormat.Mxp)).IsEqualTo("<SEND HREF=\"say \" HINT=\"Say something\" PROMPT>talk</SEND>");
await Assert.That(Render(menu, MarkupFormat.Pueblo)).IsEqualTo(
"<a xch_cmd=\"say \" xch_hint=\"Say something\" xch_mode=\"prompt\">talk</a>");
}

[Test]
public async Task AMenuLeavesOutAChoiceMxpCannotSeparate()
{
var menu = MarkupText.Wrap(
new CommandMenuMarkup([new("Pipe", "think a|b"), new("Look", "look"), new("Odd|label", "inventory")]),
MarkupText.Plain("here"));
var none = MarkupText.Wrap(new CommandMenuMarkup([new("Pipe", "think a|b")]), MarkupText.Plain("here"));

await Assert.That(Render(menu, MarkupFormat.Mxp)).IsEqualTo("<SEND HREF=look|inventory HINT=Look|Look|inventory>here</SEND>");
await Assert.That(Render(none, MarkupFormat.Mxp)).IsEqualTo("here");
}

[Test]
public async Task AMenuHintMxpCannotSeparateGivesWayToTheFirstLabel()
{
var menu = MarkupText.Wrap(
new CommandMenuMarkup([new("Look", "look"), new("Inventory", "inventory")], Hint: "Look | or not"),
MarkupText.Plain("here"));
var one = MarkupText.Wrap(new CommandMenuMarkup([new("Look", "look")], Hint: "a|b"), MarkupText.Plain("here"));

await Assert.That(Render(menu, MarkupFormat.Mxp)).IsEqualTo("<SEND HREF=look|inventory HINT=Look|Look|Inventory>here</SEND>");
await Assert.That(Render(one, MarkupFormat.Mxp)).IsEqualTo("<SEND HREF=look HINT=Look>here</SEND>");
}

// ── MXP holds each element to the client's answer ────────────────────────────

[Test]
Expand Down
8 changes: 6 additions & 2 deletions docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ line.Render(MarkupFormat.Ansi); // A map of the city The door creaks.
| `Relocate`, `LoginPrompt` | `<RELOCATE>`, `<USER>` / `<PASSWORD>` | nothing | `ms-relocate`, `ms-login` | nothing |
| `Bell` | U+0007 | U+0007 | `ms-bell` | ANSI U+0007 |

`CommandMenuMarkup` is a `ms-menu` element whose `data-commands` holds the choices as JSON,
`HighlightMarkup` a `ms-highlight` with its background, and `LinkGroupMarkup` a `ms-link-group` with
`CommandMenuMarkup` is written in MXP as one `<SEND HREF="a|b" HINT="hint|A|B">`: a click sends the
first command, and a client with menus (Mudlet, MUSHclient) offers every choice on a right-click. A choice
whose command holds `|` or a control character is left out, since MXP cannot separate it; a label holding
`|` gives way to its command. Pueblo has no menus, so it is a link to the first command (`<a xch_cmd>`, with
`xch_mode="prompt"` for `Prompt`). ANSI and Plain write the text. In HTML it is a `ms-menu` element whose
`data-commands` holds the choices as JSON, `HighlightMarkup` a `ms-highlight` with its background, and `LinkGroupMarkup` a `ms-link-group` with
its `data-group`, so an expiry can find its links.

An `ms-` element is a hook: the page decides what a clear, a pane or a gauge looks like.
Expand Down
Loading