diff --git a/CHANGELOG.md b/CHANGELOG.md index 06a15a0..5422316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `` 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 diff --git a/MarkupString.Mxp/Emitters/ElementMxpEmitter.cs b/MarkupString.Mxp/Emitters/ElementMxpEmitter.cs index eac621a..7c91f04 100644 --- a/MarkupString.Mxp/Emitters/ElementMxpEmitter.cs +++ b/MarkupString.Mxp/Emitters/ElementMxpEmitter.cs @@ -17,7 +17,7 @@ internal sealed class ElementMxpEmitter(Type markupType, Func? 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; @@ -172,12 +172,60 @@ public void Emit(IMarkup markup, ReadOnlySpan body, in EmitContext context .Close(); return; + case CommandMenuMarkup menu: + WriteMenu(menu, body, context, output); + return; + default: output.Write(body); return; } } + /// + /// A menu as one <SEND>: 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 |, + /// so a choice whose command holds one, or a control character, is left out; with none left, only the text. + /// + private static void WriteMenu(CommandMenuMarkup menu, ReadOnlySpan body, in EmitContext context, IBufferWriter 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(""); + } + + /// A choice's label for the menu; a label holding the separator gives way to the command. + 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); diff --git a/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs b/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs index ba14b34..3d90362 100644 --- a/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs +++ b/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs @@ -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), ]; /// The pane name Pueblo reads as "back to wherever text was going before". @@ -98,6 +98,24 @@ public void Emit(IMarkup markup, ReadOnlySpan 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(""); + return; + default: output.Write(body); return; diff --git a/MarkupString.Tests/SharedVocabularyTests.cs b/MarkupString.Tests/SharedVocabularyTests.cs index 7667366..52f96a6 100644 --- a/MarkupString.Tests/SharedVocabularyTests.cs +++ b/MarkupString.Tests/SharedVocabularyTests.cs @@ -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( + "Wren"); + await Assert.That(Render(menu, MarkupFormat.Pueblo)).IsEqualTo( + "Wren"); + 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(""); + await Assert.That(pueblo.Split(""); + } + + [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("talk"); + await Assert.That(Render(menu, MarkupFormat.Pueblo)).IsEqualTo( + "talk"); + } + + [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("here"); + 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("here"); + await Assert.That(Render(one, MarkupFormat.Mxp)).IsEqualTo("here"); + } + // ── MXP holds each element to the client's answer ──────────────────────────── [Test] diff --git a/docs/formats.md b/docs/formats.md index 7f2b2b5..28304e4 100644 --- a/docs/formats.md +++ b/docs/formats.md @@ -92,8 +92,12 @@ line.Render(MarkupFormat.Ansi); // A map of the city The door creaks. | `Relocate`, `LoginPrompt` | ``, `` / `` | 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 ``: 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 (``, 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.