diff --git a/CHANGELOG.md b/CHANGELOG.md index 5422316..77a014b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +### Changed + +- A Pueblo command link with a hint, and a Pueblo command menu, carry MXP's `` inside their ``. + BeipMU makes a link of `` only when it has no other attribute, and reads `` in Pueblo, so it now + gets the link, and a menu's choices on a right-click. Other Pueblo clients skip the element. Decoding reads the + pair as one link. + ### Added - `CommandMenuMarkup` is written in MXP and Pueblo. MXP gets one `` around diff --git a/MarkupString.Ansi/Emitters/AnsiEmitterSupport.cs b/MarkupString.Ansi/Emitters/AnsiEmitterSupport.cs index aa4f515..c573244 100644 --- a/MarkupString.Ansi/Emitters/AnsiEmitterSupport.cs +++ b/MarkupString.Ansi/Emitters/AnsiEmitterSupport.cs @@ -280,6 +280,14 @@ style.Foreground is not null || style.Background is not null || style.Bold || style.Faint || style.Italic || style.Underlined || style.Overlined || style.Blink || style.Inverted || style.StrikeThrough || style.Concealed; + /// Whether holds |, which <SEND> reads as a separator, or a control character. + private static bool HasSeparatorOrControl(string text) + { + foreach (var c in text) + if (c == '|' || char.IsControl(c)) return true; + return false; + } + private static void WriteTaggedLink( in AnsiStyle style, ReadOnlySpan body, @@ -302,14 +310,27 @@ private static void WriteTaggedLink( output.Write(open); output.Write(WebUtility.HtmlEncode(url)); output.Write("\""); + // BeipMU makes a link of only when it has no other attribute, so a hinted Pueblo link carries + // MXP's inside it, which BeipMU reads in Pueblo too; other Pueblo clients skip what they do not know. + var send = false; if (style.LinkText is { Length: > 0 } text) { output.Write(hint); output.Write(WebUtility.HtmlEncode(text)); output.Write("\""); + send = flavour == TagFlavour.Pueblo && !HasSeparatorOrControl(url) && !HasSeparatorOrControl(text); + if (send) + { + output.Write(">"); output.Write(body); + if (send) output.Write(""); output.Write(close); return; } diff --git a/MarkupString.Pueblo/Decoding/PuebloDecoding.cs b/MarkupString.Pueblo/Decoding/PuebloDecoding.cs index a0e7c84..75dd560 100644 --- a/MarkupString.Pueblo/Decoding/PuebloDecoding.cs +++ b/MarkupString.Pueblo/Decoding/PuebloDecoding.cs @@ -228,6 +228,15 @@ private bool Element(ParsedTag tag, string name) return true; } + // A just inside an link is the same link written again for BeipMU, which reads only + // the inner one: it takes the outer's place, and then closes nothing more. + var open = State.Open; + if (open.Count > 0 && open[^1] is ("a", { } outer)) + { + Context.Close(outer); + open[^1] = ("a", null); + } + OpenSend(name, command, tag.Get("hint")); return true; } diff --git a/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs b/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs index 3d90362..7aa4345 100644 --- a/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs +++ b/MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs @@ -99,21 +99,7 @@ public void Emit(IMarkup markup, ReadOnlySpan body, in EmitContext context 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(""); + WriteMenu(menu, body, context, output); return; default: @@ -122,6 +108,55 @@ public void Emit(IMarkup markup, ReadOnlySpan body, in EmitContext context } } + /// + /// Pueblo has no menus, so a menu is a link to its first command, as cmdlink() writes one. Inside it, a + /// <send> carries every choice the way MXP writes them, for BeipMU, which reads that element in Pueblo + /// and offers the choices on a right-click; other Pueblo clients skip an element they do not know. BeipMU makes a + /// link of <a xch_cmd> only when it has no other attribute, so the inner element is its link too. A + /// menu that puts its command on the input line has no inner element: <send> there has no prompt. + /// + private static void WriteMenu(CommandMenuMarkup menu, ReadOnlySpan body, in EmitContext context, IBufferWriter output) + { + if (menu.Choices.FirstOrDefault(choice => choice.Command.Length > 0 && !choice.Command.Any(char.IsControl)) is not { } first) + { + output.Write(body); + return; + } + + var choices = menu.Choices.Where(choice => choice.Command.Length > 0 && !HasSeparatorOrControl(choice.Command)).ToArray(); + var send = !menu.Prompt && choices.Length > 0; + + // One link around the whole text, however many runs an inner layer splits it into. + if (context.StartsRegion(menu)) + { + Tag(output, "a", ("xch_cmd", first.Command), ("xch_hint", menu.Hint ?? (first.Label.Length > 0 ? first.Label : null)), + ("xch_mode", menu.Prompt ? "prompt" : null)); + if (send) + { + // 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]); + Tag(output, "send", ("href", string.Join("|", choices.Select(choice => choice.Command))), + ("hint", choices.Length == 1 ? head : string.Join("|", [head, .. choices.Select(Label)]))); + } + } + + output.Write(body); + if (!context.EndsRegion(menu)) return; + if (send) output.Write(""); + output.Write(""); + } + + /// A choice's label in 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 static void Tag(IBufferWriter output, string name, params ReadOnlySpan<(string Name, string? Value)> attributes) { output.Write("<"); diff --git a/MarkupString.Tests/Ansi/AnsiRenderTests.cs b/MarkupString.Tests/Ansi/AnsiRenderTests.cs index 5642eb9..2595807 100644 --- a/MarkupString.Tests/Ansi/AnsiRenderTests.cs +++ b/MarkupString.Tests/Ansi/AnsiRenderTests.cs @@ -292,7 +292,7 @@ public async Task PuebloAndMxp_PlainText_IsHtmlEncoded() public async Task Pueblo_CommandLink_UsesXchCmd() { var text = MarkupText.Wrap(AnsiMarkup.Create(linkUrl: "look", linkKind: LinkKind.Command, linkText: "h"), "a"); - await Assert.That(Render(text, MarkupFormat.Pueblo)).IsEqualTo("a"); + await Assert.That(Render(text, MarkupFormat.Pueblo)).IsEqualTo("a"); } [Test] diff --git a/MarkupString.Tests/Decoding/PuebloDecodingTests.cs b/MarkupString.Tests/Decoding/PuebloDecodingTests.cs index 96a8e24..667f307 100644 --- a/MarkupString.Tests/Decoding/PuebloDecodingTests.cs +++ b/MarkupString.Tests/Decoding/PuebloDecodingTests.cs @@ -130,4 +130,17 @@ public async Task Send_WithSeveralCommands_IsAMenu() var menu = Markups(lines[0]).OfType().Single(); await Assert.That(menu.Choices).IsEquivalentTo([new CommandChoice("Buy", "buy"), new CommandChoice("Look", "look")]); } + + [Test] + public async Task ASendJustInsideALink_IsThatLinkOnce() + { + var lines = Decode("sword after
"); + var markups = Markups(lines[0]).ToArray(); + var menu = markups.OfType().Single(); + await Assert.That(menu.Choices).IsEquivalentTo([new CommandChoice("Buy", "buy"), new CommandChoice("Look", "look")]); + await Assert.That(menu.Hint).IsEqualTo("Shop"); + var sword = lines[0].Content.Runs.Single(run => run.Start == 0); + await Assert.That(sword.Markups.OfType().Count()).IsEqualTo(1); + await Assert.That(lines[0].Content.Runs.Count(run => run.Markups.Count > 0)).IsEqualTo(1); + } } diff --git a/MarkupString.Tests/SharedVocabularyTests.cs b/MarkupString.Tests/SharedVocabularyTests.cs index 52f96a6..e06bf61 100644 --- a/MarkupString.Tests/SharedVocabularyTests.cs +++ b/MarkupString.Tests/SharedVocabularyTests.cs @@ -208,7 +208,7 @@ public async Task AMenuIsOneSendInMxpAndItsFirstCommandInPueblo() await Assert.That(Render(menu, MarkupFormat.Mxp)).IsEqualTo( "Wren"); await Assert.That(Render(menu, MarkupFormat.Pueblo)).IsEqualTo( - "Wren"); + "Wren"); await Assert.That(Render(menu, MarkupFormat.Ansi)).IsEqualTo("Wren"); await Assert.That(Render(menu, MarkupFormat.Plain)).IsEqualTo("Wren"); } @@ -227,7 +227,8 @@ public async Task AMenuOverSeveralRunsIsOneLink() await Assert.That(mxp.Split(""); await Assert.That(pueblo.Split(""); + await Assert.That(pueblo.Split(""); } [Test] diff --git a/MarkupString.Tests/Snapshots/FormatSnapshotTests.EveryFixture_RendersStably_formatName=pueblo.verified.txt b/MarkupString.Tests/Snapshots/FormatSnapshotTests.EveryFixture_RendersStably_formatName=pueblo.verified.txt index c57f792..48d29c8 100644 --- a/MarkupString.Tests/Snapshots/FormatSnapshotTests.EveryFixture_RendersStably_formatName=pueblo.verified.txt +++ b/MarkupString.Tests/Snapshots/FormatSnapshotTests.EveryFixture_RendersStably_formatName=pueblo.verified.txt @@ -18,7 +18,7 @@ 18 bg-rgb | [48;2;0;128;64mon rgb[0m 19 bg-default | [49mdefault bg[0m 20 link-url | example -21 link-command | who +21 link-command | who 22 nested-three-deep | [1;4;31mdeep[0m 23 adjacent-runs | [31mred[0m-[1;32mbold green[0m[4munder[0m 24 html-send | north diff --git a/docs/formats.md b/docs/formats.md index 28304e4..9f60a7a 100644 --- a/docs/formats.md +++ b/docs/formats.md @@ -30,7 +30,7 @@ var link = MarkupText.Wrap( MarkupText.Wrap(AnsiCodeParser.Parse("hr"), "north")); link.Render(MarkupFormat.Html); // north -link.Render(MarkupFormat.Pueblo); // \e[1;31mnorth\e[0m +link.Render(MarkupFormat.Pueblo); // \e[1;31mnorth\e[0m link.Render(MarkupFormat.Mxp); // \e[1;31mnorth\e[0m link.Render(MarkupFormat.Ansi); // \e[1;31mnorth\e[0m link.Render(MarkupFormat.BBCode); // [color=#ff5555]north[/color] @@ -96,7 +96,9 @@ line.Render(MarkupFormat.Ansi); // A map of the city The door creaks. 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 +`xch_mode="prompt"` for `Prompt`). Inside the link, a `` carries the choices as MXP writes them, for +BeipMU, which reads that element in Pueblo and offers the choices on a right-click; other Pueblo clients skip +it. A `Prompt` menu has none. 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. @@ -160,7 +162,7 @@ spelled alike, but links are not, and each client prints the other's tags as tex | | Pueblo | MXP | Html (the portal's terminal) | |---|---|---|---| -| Command link | `` | `` | `` | +| Command link | ``, with `` inside for BeipMU | `` | `` | | URL link | `` | `` | `` | | Tag read on | any line | a line opened in secure mode (`ESC[1z`) | — |