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

### Changed

- A Pueblo command link with a hint, and a Pueblo command menu, carry MXP's `<SEND>` inside their `<a xch_cmd>`.
BeipMU makes a link of `<a xch_cmd>` only when it has no other attribute, and reads `<SEND>` 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 `<SEND HREF="a|b" HINT="hint|A|B">` around
Expand Down
21 changes: 21 additions & 0 deletions MarkupString.Ansi/Emitters/AnsiEmitterSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>Whether <paramref name="text"/> holds <c>|</c>, which <c>&lt;SEND&gt;</c> reads as a separator, or a control character.</summary>
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<char> body,
Expand All @@ -302,14 +310,27 @@ private static void WriteTaggedLink(
output.Write(open);
output.Write(WebUtility.HtmlEncode(url));
output.Write("\"");
// BeipMU makes a link of <A XCH_CMD> only when it has no other attribute, so a hinted Pueblo link carries
// MXP's <SEND> 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("><SEND HREF=\"");
output.Write(WebUtility.HtmlEncode(url));
output.Write("\" HINT=\"");
output.Write(WebUtility.HtmlEncode(text));
output.Write("\"");
}
}
output.Write(">");
output.Write(body);
if (send) output.Write("</SEND>");
output.Write(close);
return;
}
Expand Down
9 changes: 9 additions & 0 deletions MarkupString.Pueblo/Decoding/PuebloDecoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ private bool Element(ParsedTag tag, string name)
return true;
}

// A <send> just inside an <a> link is the same link written again for BeipMU, which reads only
// the inner one: it takes the outer's place, and </a> 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;
}
Expand Down
65 changes: 50 additions & 15 deletions MarkupString.Pueblo/Emitters/ElementPuebloEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,7 @@ public void Emit(IMarkup markup, ReadOnlySpan<char> 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("</a>");
WriteMenu(menu, body, context, output);
return;

default:
Expand All @@ -122,6 +108,55 @@ public void Emit(IMarkup markup, ReadOnlySpan<char> body, in EmitContext context
}
}

/// <summary>
/// Pueblo has no menus, so a menu is a link to its first command, as <c>cmdlink()</c> writes one. Inside it, a
/// <c>&lt;send&gt;</c> 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 <c>&lt;a xch_cmd&gt;</c> 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: <c>&lt;send&gt;</c> there has no prompt.
/// </summary>
private static void WriteMenu(CommandMenuMarkup menu, ReadOnlySpan<char> body, in EmitContext context, IBufferWriter<char> 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("</send>");
output.Write("</a>");
}

/// <summary>A choice's label in 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 static void Tag(IBufferWriter<char> output, string name, params ReadOnlySpan<(string Name, string? Value)> attributes)
{
output.Write("<");
Expand Down
2 changes: 1 addition & 1 deletion MarkupString.Tests/Ansi/AnsiRenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 XCH_CMD=\"look\" XCH_HINT=\"h\">a</A>");
await Assert.That(Render(text, MarkupFormat.Pueblo)).IsEqualTo("<A XCH_CMD=\"look\" XCH_HINT=\"h\"><SEND HREF=\"look\" HINT=\"h\">a</SEND></A>");
}

[Test]
Expand Down
13 changes: 13 additions & 0 deletions MarkupString.Tests/Decoding/PuebloDecodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,17 @@ public async Task Send_WithSeveralCommands_IsAMenu()
var menu = Markups(lines[0]).OfType<CommandMenuMarkup>().Single();
await Assert.That(menu.Choices).IsEquivalentTo([new CommandChoice("Buy", "buy"), new CommandChoice("Look", "look")]);
}

[Test]
public async Task ASendJustInsideALink_IsThatLinkOnce()
{
var lines = Decode("</xch_mudtext><a xch_cmd=\"buy\" xch_hint=\"Shop\"><send href=\"buy|look\" hint=\"Shop|Buy|Look\">sword</send></a> after<br>");
var markups = Markups(lines[0]).ToArray();
var menu = markups.OfType<CommandMenuMarkup>().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<ElementMarkup>().Count()).IsEqualTo(1);
await Assert.That(lines[0].Content.Runs.Count(run => run.Markups.Count > 0)).IsEqualTo(1);
Comment thread
HarryCordewener marked this conversation as resolved.
Resolved
}
}
5 changes: 3 additions & 2 deletions MarkupString.Tests/SharedVocabularyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public async Task AMenuIsOneSendInMxpAndItsFirstCommandInPueblo()
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>");
"<a xch_cmd=\"look Wren\" xch_hint=\"Wren\"><send href=\"look Wren|page Wren=\" hint=\"Wren|Look|Page\">Wren</send></a>");
await Assert.That(Render(menu, MarkupFormat.Ansi)).IsEqualTo("Wren");
await Assert.That(Render(menu, MarkupFormat.Plain)).IsEqualTo("Wren");
}
Expand All @@ -227,7 +227,8 @@ public async Task AMenuOverSeveralRunsIsOneLink()
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>");
await Assert.That(pueblo.Split("<send ").Length).IsEqualTo(2);
await Assert.That(pueblo).EndsWith("en</send></a>");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
18 bg-rgb | <ESC>[48;2;0;128;64mon rgb<ESC>[0m
19 bg-default | <ESC>[49mdefault bg<ESC>[0m
20 link-url | <A HREF="https://example.com/a?b=1&amp;c=2">example</A>
21 link-command | <A XCH_CMD="+who" XCH_HINT="Who is on?">who</A>
21 link-command | <A XCH_CMD="+who" XCH_HINT="Who is on?"><SEND HREF="+who" HINT="Who is on?">who</SEND></A>
22 nested-three-deep | <ESC>[1;4;31mdeep<ESC>[0m
23 adjacent-runs | <ESC>[31mred<ESC>[0m-<ESC>[1;32mbold green<ESC>[0m<ESC>[4munder<ESC>[0m
24 html-send | <send href="north" hint="Go north">north</send>
Expand Down
8 changes: 5 additions & 3 deletions docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var link = MarkupText.Wrap(
MarkupText.Wrap(AnsiCodeParser.Parse("hr"), "north"));

link.Render(MarkupFormat.Html); // <span style="color: #ff5555"><a class="ms-cmd-link" role="button" tabindex="0" xch_cmd="north" title="Go north">north</a></span>
link.Render(MarkupFormat.Pueblo); // \e[1;31m<A XCH_CMD="north" XCH_HINT="Go north">north</A>\e[0m
link.Render(MarkupFormat.Pueblo); // \e[1;31m<A XCH_CMD="north" XCH_HINT="Go north"><SEND HREF="north" HINT="Go north">north</SEND></A>\e[0m
link.Render(MarkupFormat.Mxp); // \e[1;31m<SEND HREF="north" HINT="Go north">north</SEND>\e[0m
link.Render(MarkupFormat.Ansi); // \e[1;31mnorth\e[0m
link.Render(MarkupFormat.BBCode); // [color=#ff5555]north[/color]
Expand Down Expand Up @@ -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 (`<a xch_cmd>`, 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 `<send>` 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.

Expand Down Expand Up @@ -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 | `<A XCH_CMD="cmd" XCH_HINT="hint">` | `<SEND HREF="cmd" HINT="hint">` | `<a class="ms-cmd-link" xch_cmd="cmd" title="hint">` |
| Command link | `<A XCH_CMD="cmd" XCH_HINT="hint">`, with `<SEND HREF="cmd" HINT="hint">` inside for BeipMU | `<SEND HREF="cmd" HINT="hint">` | `<a class="ms-cmd-link" xch_cmd="cmd" title="hint">` |
| URL link | `<A HREF="url">` | `<A HREF="url">` | `<a href="url">` |
| Tag read on | any line | a line opened in secure mode (`ESC[1z`) | — |

Expand Down
Loading