diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1bdf807..a9507ae 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -125,7 +125,7 @@ jobs: run: | v="${GITHUB_REF_NAME#v}" awk -v v="$v" '/^## /{p = index($0, "## " v) == 1; next} p' CHANGELOG.md > notes.md - # The same news in Spanish and English, between markers the app reads to tell what a new version brings before updating. + # The same news in Spanish, English, German and Korean, each between its own markers the app reads to tell what a new version brings before updating. awk -v v="$v" '/^## /{p = $0 == "## " v; next} p' docs/novedades.md > es.md if grep -q '^- ' es.md; then { @@ -152,6 +152,32 @@ jobs: echo "" } >> notes.md fi + awk -v v="$v" '/^## /{p = $0 == "## " v; next} p' docs/novedades.de.md > de.md + if grep -q '^- ' de.md; then + { + echo + echo "
Neuigkeiten auf Deutsch" + echo + echo "" + cat de.md + echo "" + echo + echo "
" + } >> notes.md + fi + awk -v v="$v" '/^## /{p = $0 == "## " v; next} p' docs/novedades.ko.md > ko.md + if grep -q '^- ' ko.md; then + { + echo + echo "
한국어 변경 사항" + echo + echo "" + cat ko.md + echo "" + echo + echo "
" + } >> notes.md + fi base="https://github.com/${GITHUB_REPOSITORY}/releases/download/${GITHUB_REF_NAME}" exe=$(cut -d' ' -f1 dist/Stackshot.exe.sha256) msi=$(cut -d' ' -f1 dist/Stackshot.msi.sha256) diff --git a/src/Home/HomeMascotPage.cs b/src/Home/HomeMascotPage.cs index a16d8dd..415ebc4 100644 --- a/src/Home/HomeMascotPage.cs +++ b/src/Home/HomeMascotPage.cs @@ -753,13 +753,14 @@ FrameworkElement StageName() TextBox box = new TextBox { - Width = 236, Height = 38, MaxLength = 16, HorizontalAlignment = HorizontalAlignment.Left, Visibility = Visibility.Collapsed, + Width = 236, Height = 38, HorizontalAlignment = HorizontalAlignment.Left, Visibility = Visibility.Collapsed, FontFamily = Ds.Display, FontWeight = FontWeights.SemiBold, FontSize = 22, TextAlignment = TextAlignment.Left, VerticalContentAlignment = VerticalAlignment.Center, Padding = new Thickness(4, 0, 8, 0), Foreground = Ds.Brush(pal.Label), CaretBrush = Ds.Brush(pal.Accent), SelectionBrush = Ds.Brush(pal.Accent), Background = Ds.Brush(pal.High ? pal.Window : pal.Dark ? Ds.Argb(0.08, 255, 255, 255) : Ds.Argb(0.8, 255, 255, 255)), BorderBrush = Ds.Brush(pal.Accent), BorderThickness = new Thickness(1.5), Template = HeroFieldTemplate() }; + MascotName.Guard(box); slot.Children.Add(box); stageName = name; @@ -835,11 +836,7 @@ void SetMascotName(string text) static string CleanName(string text) { - StringBuilder b = new StringBuilder(); - foreach (char c in text ?? "") if (!char.IsControl(c)) b.Append(c); - string v = b.ToString().Trim(); - if (v.Length > 16) v = v.Substring(0, char.IsHighSurrogate(v[15]) ? 15 : 16).Trim(); - if (v.Length > 0 && char.IsHighSurrogate(v[v.Length - 1])) v = v.Substring(0, v.Length - 1).Trim(); // never half an emoji + string v = MascotName.Clean(text); return v.Length > 0 ? v : "Pixel"; } diff --git a/src/MascotName.cs b/src/MascotName.cs new file mode 100644 index 0000000..8ef54a9 --- /dev/null +++ b/src/MascotName.cs @@ -0,0 +1,97 @@ +// Stackshot - The mascot's name, whatever the keyboard, the settings file or a paste brings: no control or bidirectional +// override characters, at most 16 UTF-16 units, and never cut through a letter that is several pieces (an emoji with its +// joiners, a flag, a Devanagari or Bengali conjunct with its vowel signs). +// MIT License - https://github.com/rubenitx/stackshot +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace Stackshot +{ + public static class MascotName + { + public const int MaxUnits = 16; + + // What the name may not contain: control characters and the characters that turn the text around (U+202A-U+202E + // embeddings and overrides, U+2066-U+2069 isolates), which would make a name read as if it were another one. + static bool Forbidden(char c) + { + return char.IsControl(c) || (c >= '\u202A' && c <= '\u202E') || (c >= '\u2066' && c <= '\u2069'); + } + + // The typed text without what is forbidden and cut to the limit: still as typed otherwise, spaces included. + public static string Limit(string text) + { + StringBuilder b = new StringBuilder(); + foreach (char c in text ?? "") if (!Forbidden(c)) b.Append(c); + string v = b.ToString(); + if (v.Length <= MaxUnits) return v; + StringBuilder o = new StringBuilder(); + foreach (string piece in Pieces(v)) + { + if (o.Length + piece.Length > MaxUnits) break; + o.Append(piece); + } + return o.ToString(); + } + + // A field the name is typed or pasted in: what is forbidden never stays, and the text stops at the limit without + // cutting a letter in half (the box's own MaxLength counts units and would). + public static void Guard(System.Windows.Controls.TextBox box) + { + box.TextChanged += delegate + { + string v = Limit(box.Text); + if (v == box.Text) return; + int caret = box.CaretIndex; + box.Text = v; + box.CaretIndex = System.Math.Min(caret, v.Length); + }; + } + + // The name to keep: Limit, with no spaces around it ("" when nothing is left; the caller picks the default). + public static string Clean(string text) + { + StringBuilder b = new StringBuilder(); + foreach (char c in text ?? "") if (!Forbidden(c)) b.Append(c); + return Limit(b.ToString().Trim()).Trim(); + } + + // The text split in what is read as one letter each. + static List Pieces(string v) + { + List pieces = new List(); + TextElementEnumerator e = StringInfo.GetTextElementEnumerator(v); + while (e.MoveNext()) + { + string t = e.GetTextElement(); + if (pieces.Count > 0 && Joins(pieces[pieces.Count - 1], t)) pieces[pieces.Count - 1] += t; + else pieces.Add(t); + } + return pieces; + } + + // The framework's rules for what belongs together predate emoji sequences and conjuncts: a piece that follows a + // joiner or a virama, starts with a joiner or is a skin tone, or the second half of a flag, stays with the one before. + static bool Joins(string before, string next) + { + char last = before[before.Length - 1]; + if (last == '\u200D' || next[0] == '\u200D' || IsVirama(last)) return true; + if (next.Length == 2 && next[0] == '\uD83C' && next[1] >= '\uDFFB' && next[1] <= '\uDFFF') return true; + return before.Length == 2 && IsRegional(before) && next.Length == 2 && IsRegional(next); + } + + static bool IsRegional(string s) { return s[0] == '\uD83C' && s[1] >= '\uDDE6' && s[1] <= '\uDDFF'; } + + // The vowel killers of the Indic scripts, which tie the next consonant to the previous one. + static bool IsVirama(char c) + { + switch (c) + { + case '\u094D': case '\u09CD': case '\u0A4D': case '\u0ACD': case '\u0B4D': case '\u0BCD': + case '\u0C4D': case '\u0CCD': case '\u0D4D': case '\u0DCA': case '\u1039': return true; + } + return false; + } + } +} diff --git a/src/News.cs b/src/News.cs index d7a21e4..1e504d9 100644 --- a/src/News.cs +++ b/src/News.cs @@ -21,6 +21,9 @@ public sealed class Item public const string Start = "", End = ""; // The same news in English, which the release workflow writes beside the Spanish ones. public const string StartEn = "", EndEn = ""; + // And in German and Korean, each between its own markers. + public const string StartDe = "", EndDe = ""; + public const string StartKo = "", EndKo = ""; static string embedded, testEn, testDe, testKo; static bool loaded; @@ -127,6 +130,10 @@ static List Section(string md, string version) // The same news in English (empty when the release only carries the Spanish ones). public static List FromReleaseEnglish(string body) { return Between(body, StartEn, EndEn); } + // The same news in German or Korean (empty when the release does not carry them: releases before 2.2.2). + public static List FromReleaseGerman(string body) { return Between(body, StartDe, EndDe); } + public static List FromReleaseKorean(string body) { return Between(body, StartKo, EndKo); } + static List Between(string body, string start, string end) { if (string.IsNullOrEmpty(body)) return new List(); diff --git a/src/Settings.cs b/src/Settings.cs index 7116b52..e9cbe7a 100644 --- a/src/Settings.cs +++ b/src/Settings.cs @@ -275,7 +275,7 @@ int Parse(string[] lines) case "showintro": s.ShowIntro = Bool(v, s.ShowIntro); break; case "languageoffered": s.LanguageOffered = Bool(v, s.LanguageOffered); break; case "mascot": s.MascotOnSaved = Bool(v, s.MascotOnSaved); break; - case "mascotname": if (v.Length > 0) s.MascotName = Cut(v, 16); break; + case "mascotname": { string n = global::Stackshot.MascotName.Clean(v); if (n.Length > 0) s.MascotName = n; break; } case "mascotcolor": s.MascotColor = Int(v, s.MascotColor, 0, 99); break; case "mascottalks": s.MascotTalks = Bool(v, s.MascotTalks); break; case "mascotkind": s.MascotKind = Int(v, s.MascotKind, 0, 99); break; diff --git a/src/Updater.cs b/src/Updater.cs index be57316..28d9970 100644 --- a/src/Updater.cs +++ b/src/Updater.cs @@ -57,13 +57,18 @@ public class Release public List Notes = new List(); public List News = new List(); // in Spanish, when the release carries them public List NewsEn = new List(); // the same in English, when the release carries them + public List NewsDe = new List(); // in German + public List NewsKo = new List(); // in Korean // What a notice tells of this release in the interface language. The news between the markers are Spanish - // (and English when the workflow wrote them), so Spanish reads those and every other language the English - // ones, or the changelog lines the release always has in English: never the Spanish news. + // (and English, German and Korean when the workflow wrote them): each of those languages reads its own, every + // other the English ones, and a release that lacks the block falls back to the English ones and then to the + // changelog lines it always has in English: never the Spanish news in another language. public List Items() { if (I18n.IsSpanish && News.Count > 0) return News; + if (I18n.IsGerman && NewsDe.Count > 0) return NewsDe; + if (I18n.IsKorean && NewsKo.Count > 0) return NewsKo; if (!I18n.IsSpanish && NewsEn.Count > 0) return NewsEn; List items = new List(); foreach (string n in Notes) items.Add(new News.Item { Title = n, Text = "" }); @@ -463,10 +468,12 @@ static Release Parse(string json) string body = Str(root, "body") ?? ""; r.News = global::Stackshot.News.FromRelease(body); r.NewsEn = global::Stackshot.News.FromReleaseEnglish(body); + r.NewsDe = global::Stackshot.News.FromReleaseGerman(body); + r.NewsKo = global::Stackshot.News.FromReleaseKorean(body); foreach (string raw in body.Replace("\r", "").Split('\n')) { string line = raw.Trim(); - if (line.StartsWith("###") || line.StartsWith("]+>", ""); int dot = line.IndexOf(". "); diff --git a/tests/app/AppTestsI18nAudit.cs b/tests/app/AppTestsI18nAudit.cs index ab5acb7..deb0e0d 100644 --- a/tests/app/AppTestsI18nAudit.cs +++ b/tests/app/AppTestsI18nAudit.cs @@ -42,7 +42,7 @@ public static void SettingsRowsNotSqueezed() if (t == null || string.IsNullOrEmpty(t.Text) || t.ActualWidth <= 0) return; // A title that ends in an ellipsis loses its end: its natural width is wider than the room it got. // (A row named after the user's own folder or device, "Windows default: Speakers (Model)", runs as long as that name.) - bool theirs = t.Text.Contains(":\\") || Regex.IsMatch(t.Text, @": .+ \(.+\)$"); + bool theirs = IsTheirs(t.Text); if (!theirs && t.TextTrimming != TextTrimming.None && t.TextWrapping == TextWrapping.NoWrap) { TextBlock whole = new TextBlock { Text = t.Text, FontFamily = t.FontFamily, FontSize = t.FontSize, FontWeight = t.FontWeight, FontStyle = t.FontStyle, FontStretch = t.FontStretch }; @@ -64,6 +64,13 @@ public static void SettingsRowsNotSqueezed() Check(cut.Count == 0, cut.Count + " settings titles end in an ellipsis"); } + // A row named after what the user owns: a folder (as "C:\...", or "~\..." under their profile) or a device, + // "Windows default: Speakers (Model)": it runs as long as that name, whatever the language. + internal static bool IsTheirs(string text) + { + return text.Contains(":\\") || text.Contains("~\\") || Regex.IsMatch(text, @": .+ \(.+\)$"); + } + // Texts that are the same in every language: formats and product names. static readonly string[] SameEverywhere = { "Markdown", "GIF", "PDF", "OK", "HTML", "XML", "OCR", "Stackshot" }; @@ -111,7 +118,7 @@ static void ListsLeak(string lang) const string SpanishHeadline = "Titular s\u00F3lo en espa\u00F1ol"; - static Updater.Release ParseRelease(string body) + internal static Updater.Release ParseRelease(string body) { string json = "{\"tag_name\":\"v9.9.9\",\"body\":" + new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(body) + "}"; return (Updater.Release)typeof(Updater).GetMethod("Parse", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { json }); @@ -303,7 +310,7 @@ public static void OldReleaseBodiesStillRead() Updater.Release r = ParseRelease(b.Value); bool hasEs = b.Key == "spanish markers only" || b.Key == "crlf"; Check((r.News.Count == 1) == hasEs, b.Key + ": Spanish news read = " + r.News.Count); - Check(r.NewsEn.Count == 0, b.Key + ": English news appeared from nowhere"); + Check(r.NewsEn.Count == 0 && r.NewsDe.Count == 0 && r.NewsKo.Count == 0, b.Key + ": English, German or Korean news appeared from nowhere"); I18n.Apply("es"); List es = r.Items(); if (hasEs) Check(es.Count == 1 && es[0].Title.Contains(SpanishHeadline), b.Key + ": Spanish does not read the Spanish news"); @@ -332,9 +339,20 @@ public static void WorkflowWritesTheMarkers() string yml = File.ReadAllText(Path.Combine(root, ".github", "workflows", "build.yml")); foreach (string marker in new[] { News.Start, News.End, News.StartEn, News.EndEn }) Check(yml.Contains("echo \"" + marker + "\""), "build.yml does not write " + marker); - Check(yml.Contains("docs/novedades.md") && yml.Contains("docs/novedades.en.md"), "build.yml does not read both news files"); - Check(yml.IndexOf(News.Start, StringComparison.Ordinal) < yml.IndexOf(News.StartEn, StringComparison.Ordinal), "the Spanish block is written before the English one"); - foreach (string file in new[] { "novedades.md", "novedades.en.md" }) + foreach (string marker in new[] { News.StartDe, News.EndDe, News.StartKo, News.EndKo }) + Check(yml.Contains("echo \"" + marker + "\""), "build.yml does not write " + marker); + foreach (string file in new[] { "novedades.md", "novedades.en.md", "novedades.de.md", "novedades.ko.md" }) + Check(yml.Contains("docs/" + file), "build.yml does not read docs/" + file); + string[] order = { News.Start, News.StartEn, News.StartDe, News.StartKo }; + for (int i = 1; i < order.Length; i++) + Check(yml.IndexOf(order[i - 1], StringComparison.Ordinal) < yml.IndexOf(order[i], StringComparison.Ordinal), "the blocks are written in the order es, en, de, ko"); + // What the release job is allowed to do and run on does not change: only a v* tag releases, with the same + // permissions, and every action stays pinned to a commit. + Check(yml.Contains("if: startsWith(github.ref, 'refs/tags/v')"), "the release job no longer runs only for v* tags"); + Check(yml.Contains("contents: write") && yml.Contains("id-token: write") && yml.Contains("attestations: write"), "the release permissions changed"); + foreach (Match u in Regex.Matches(yml, @"uses: (\S+)")) + Check(Regex.IsMatch(u.Groups[1].Value, @"@[0-9a-f]{40}$"), "an action is not pinned to a commit: " + u.Groups[1].Value); + foreach (string file in new[] { "novedades.md", "novedades.en.md", "novedades.de.md", "novedades.ko.md" }) { string[] lines = File.ReadAllLines(Path.Combine(root, "docs", file)); int head = Array.FindIndex(lines, l => Regex.IsMatch(l, @"^## \d+\.\d+\.\d+\s*$"));