diff --git a/OneMore/AddInCommands.cs b/OneMore/AddInCommands.cs index be00156385..718519193e 100644 --- a/OneMore/AddInCommands.cs +++ b/OneMore/AddInCommands.cs @@ -990,6 +990,11 @@ public async Task TrimLeadingCmd(IRibbonControl control) => await factory.Run(true); + [Command("ribUnnameUrlsButton_Label", Keys.None, "ribReferencesMenu")] + public async Task UnnameUrlsCmd(IRibbonControl control) + => await factory.Run(); + + [Command("ribUpdatePageTimeButton_Label", Keys.None, "ribPageMenu")] public async Task UpdatePageTimeCmd(IRibbonControl control) => await factory.Run(true); diff --git a/OneMore/Commands/References/NameUrlsCommand.cs b/OneMore/Commands/References/NameUrlsCommand.cs index 45e4cb0dc5..18930c86e3 100644 --- a/OneMore/Commands/References/NameUrlsCommand.cs +++ b/OneMore/Commands/References/NameUrlsCommand.cs @@ -1,5 +1,5 @@ //************************************************************************************************ -// Copyright © 2020 Steven M Cohn. All rights reserved. +// Copyright © 2020 Steven M Cohn. All rights reserved. //************************************************************************************************ namespace River.OneMoreAddIn.Commands @@ -17,6 +17,18 @@ namespace River.OneMoreAddIn.Commands using System.Xml.Linq; + #region wrappers + internal class UnnameUrlsCommand : NameUrlsCommand + { + public UnnameUrlsCommand() { } + public override Task Execute(params object[] args) + { + return base.Execute(false); + } + } + #endregion + + internal class NameUrlsCommand : Command { @@ -27,48 +39,95 @@ public NameUrlsCommand() public override async Task Execute(params object[] args) { - if (!HttpClientFactory.IsNetworkAvailable()) + var friendly = args.Length == 0 || (bool)args[0]; + + if (friendly && !HttpClientFactory.IsNetworkAvailable()) { ShowInfo(Properties.Resources.NetwordConnectionUnavailable); return; } await using var one = new OneNote(out var page, out _); - if (await NameUrls(page)) + + var updated = friendly + ? await NameUrls(page) + : SimplifyUrls(page); + + if (updated) { await one.Update(page); } } - private async Task NameUrls(Page page) + private static bool SimplifyUrls(Page page) + { + var elements = GetCandiateElements(page); + + var count = 0; + foreach (var element in elements) + { + var cdata = element.GetCData(); + var wrapper = cdata.GetWrapper(); + var anchor = wrapper.Element("a"); + if (anchor is not null) + { + var href = anchor.Attribute("href")?.Value; + if (!string.IsNullOrWhiteSpace(href)) + { + if (anchor.TextValue() != href) + { + anchor.Value = href; + cdata.Value = anchor.ToString(SaveOptions.DisableFormatting); + count++; + } + } + } + } + + return count > 0; + } + + + private static List GetCandiateElements(Page page) { - List elements = null; + List elements; + + // OneNote XML will insert CR prior to 'href' in the CDATA var regex = new Regex(@" - e.Attributes("selected").Any(a => a.Value.Equals("all"))); + var range = new SelectionRange(page); + range.GetSelection(); - if ((selections.Count() == 1) && - (selections.First().DescendantNodes().OfType().First().Value.Length == 0)) + if (range.Scope == SelectionScope.None || + range.Scope == SelectionScope.TextCursor) { - // single empty selection so affect entire page - elements = page.Root.DescendantNodes().OfType() + // entire page + elements = page.Root + .DescendantNodes().OfType() .Where(c => regex.IsMatch(c.Value)) .Select(e => e.Parent) .ToList(); } else { - // selected range so affect only within that - elements = page.Root.DescendantNodes().OfType() + // only selections + elements = page.Root + .DescendantNodes().OfType() .Where(c => regex.IsMatch(c.Value)) .Select(e => e.Parent) .Where(e => e.Attributes("selected").Any(a => a.Value == "all")) .ToList(); } + return elements; + } + + + private async Task NameUrls(Page page) + { + var elements = GetCandiateElements(page); + // parallelize internet access for all hyperlinks on page... int count = 0; @@ -97,7 +156,7 @@ private async Task ReplaceUrlText(XElement element) var wrapper = cdata.GetWrapper(); var anchor = wrapper.Element("a"); - if (anchor == null) + if (anchor is null) { return 0; } @@ -168,7 +227,7 @@ private async Task FetchPageTitle(string url) var contents = ""; var length = 0; - while ((title == null) && (length = await stream.ReadAsync(buffer, 0, chunkSize)) > 0) + while ((title is null) && (length = await stream.ReadAsync(buffer, 0, chunkSize)) > 0) { // convert the byte-array to a string and add it to the rest of the // contents that have been downloaded so far diff --git a/OneMore/Properties/Resources.Designer.cs b/OneMore/Properties/Resources.Designer.cs index 4487038d0d..bc671d8fe4 100644 --- a/OneMore/Properties/Resources.Designer.cs +++ b/OneMore/Properties/Resources.Designer.cs @@ -10489,6 +10489,24 @@ internal static string ribTrimLeadingButton_Screentip { } } + /// + /// Looks up a localized string similar to Replace URLs with Web Address. + /// + internal static string ribUnnameUrlsButton_Label { + get { + return ResourceManager.GetString("ribUnnameUrlsButton_Label", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Replace URL titles with basic Web address. + /// + internal static string ribUnnameUrlsButton_Screentip { + get { + return ResourceManager.GetString("ribUnnameUrlsButton_Screentip", resourceCulture); + } + } + /// /// Looks up a localized string similar to Update Page Time. /// diff --git a/OneMore/Properties/Resources.ar-SA.resx b/OneMore/Properties/Resources.ar-SA.resx index 3f3caf2e05..deb46d33de 100644 --- a/OneMore/Properties/Resources.ar-SA.resx +++ b/OneMore/Properties/Resources.ar-SA.resx @@ -4243,6 +4243,14 @@ ISO-code then comma then language name اقتطاع المسافة البيضاء البادئة من النص المحدد ribbon item tootp + + استبدل عناوين URL بعنوان الويب + ribbon reference + + + استبدل عناوين URL بعنوان الويب الأساسي + ribbon reference + تحديث وقت الصفحة page menu diff --git a/OneMore/Properties/Resources.de-DE.resx b/OneMore/Properties/Resources.de-DE.resx index b3665e9f50..4436ce5301 100644 --- a/OneMore/Properties/Resources.de-DE.resx +++ b/OneMore/Properties/Resources.de-DE.resx @@ -4239,6 +4239,14 @@ Nach der letzten Gruppe Entfernt führende Leerzeichen aus ausgewähltem Text ribbon edit + + Ersetzen Sie URLs durch Webadressen + ribbon reference + + + Ersetzen Sie URL-Titel durch einfache Webadressen + ribbon reference + Aktualisieren Sie die Seitenzeit page menu diff --git a/OneMore/Properties/Resources.es-ES.resx b/OneMore/Properties/Resources.es-ES.resx index 6201cd84f8..c3596efbfd 100644 --- a/OneMore/Properties/Resources.es-ES.resx +++ b/OneMore/Properties/Resources.es-ES.resx @@ -4243,6 +4243,14 @@ Después del último grupo Recortar los espacios en blanco iniciales del texto seleccionado ribbon item tootp + + Reemplazar URL con dirección web + ribbon reference + + + Reemplazar títulos de URL con direcciones web básicas + ribbon reference + Hora de actualización de la página page menu diff --git a/OneMore/Properties/Resources.fr-FR.resx b/OneMore/Properties/Resources.fr-FR.resx index ec963b3588..dfb7dbe6b0 100644 --- a/OneMore/Properties/Resources.fr-FR.resx +++ b/OneMore/Properties/Resources.fr-FR.resx @@ -4242,6 +4242,14 @@ Après le dernier groupe Couper les espaces blancs de début du texte sélectionné ribbon item tootp + + Remplacer les URL par une adresse Web + ribbon reference + + + Remplacer les titres d'URL par une adresse Web de base + ribbon reference + Mettre à jour l'heure de la page page menu diff --git a/OneMore/Properties/Resources.he-IL.resx b/OneMore/Properties/Resources.he-IL.resx index d0bd7fa14c..41cab3a633 100644 --- a/OneMore/Properties/Resources.he-IL.resx +++ b/OneMore/Properties/Resources.he-IL.resx @@ -4242,6 +4242,14 @@ ISO-code then comma then language name חתוך את הרווח הלבן המוביל מהטקסט שנבחר ribbon item tootp + + החלף כתובות URL בכתובת אינטרנט + ribbon reference + + + החלף את כותרות הכתובות בכתובת אינטרנט בסיסית + ribbon reference + עדכן את זמן העמוד page menu diff --git a/OneMore/Properties/Resources.ja-JP.resx b/OneMore/Properties/Resources.ja-JP.resx index f6b7ed0c4d..8b1be56616 100644 --- a/OneMore/Properties/Resources.ja-JP.resx +++ b/OneMore/Properties/Resources.ja-JP.resx @@ -4247,6 +4247,14 @@ ISO-code then comma then language name 選択したテキストから先頭のホワイトスペースを削除する ribbon edit + + URL を Web アドレスに置き換えます + ribbon reference + + + URL タイトルを基本的な Web アドレスに置き換えます + ribbon reference + ページ時間の更新 page menu diff --git a/OneMore/Properties/Resources.nl-NL.resx b/OneMore/Properties/Resources.nl-NL.resx index add3e1a1bf..49575a99c3 100644 --- a/OneMore/Properties/Resources.nl-NL.resx +++ b/OneMore/Properties/Resources.nl-NL.resx @@ -4243,6 +4243,14 @@ Na de laatste groep Snijd de witruimte voor uit de geselecteerde tekst ribbon item tootp + + Vervang URL's door een webadres + ribbon reference + + + Vervang URL-titels door een standaard webadres + ribbon reference + Paginatijd bijwerken page menu diff --git a/OneMore/Properties/Resources.pl-PL.resx b/OneMore/Properties/Resources.pl-PL.resx index bba94e13b1..6f1b62a0dd 100644 --- a/OneMore/Properties/Resources.pl-PL.resx +++ b/OneMore/Properties/Resources.pl-PL.resx @@ -4244,6 +4244,14 @@ Po ostatniej grupie Wykończenia prowadzące białe znaki z wybranego tekstu ribbon item tootp + + Zastąp adresy URL adresem internetowym + ribbon reference + + + Zastąp tytuły adresów URL podstawowym adresem internetowym + ribbon reference + Zaktualizuj czas strony page menu diff --git a/OneMore/Properties/Resources.pt-BR.resx b/OneMore/Properties/Resources.pt-BR.resx index ac8ee1a53c..e16c7845d7 100644 --- a/OneMore/Properties/Resources.pt-BR.resx +++ b/OneMore/Properties/Resources.pt-BR.resx @@ -4243,6 +4243,14 @@ Depois do último grupo Cortar o espaço em branco inicial do texto selecionado ribbon item tootp + + Substitua URLs por endereço da web + ribbon reference + + + Substitua os títulos de URL por endereços da Web básicos + ribbon reference + Atualizar tempo da página page menu diff --git a/OneMore/Properties/Resources.resx b/OneMore/Properties/Resources.resx index 1eb4741922..c7f4070a0e 100644 --- a/OneMore/Properties/Resources.resx +++ b/OneMore/Properties/Resources.resx @@ -4245,6 +4245,14 @@ After last group Trim leading whitespace from selected text ribbon edit + + Replace URLs with Web Address + ribbon reference + + + Replace URL titles with basic Web address + ribbon reference + Update Page Time page menu diff --git a/OneMore/Properties/Resources.zh-CN.resx b/OneMore/Properties/Resources.zh-CN.resx index 8ebede7d1a..a3edd53c20 100644 --- a/OneMore/Properties/Resources.zh-CN.resx +++ b/OneMore/Properties/Resources.zh-CN.resx @@ -4236,6 +4236,14 @@ ISO-code then comma then language name 修剪所选文本中的前导空白 ribbon item tootp + + 将 URL 替换为网址 + ribbon reference + + + 将 URL 标题替换为基本网址 + ribbon reference + 更新页面时间 page menu diff --git a/OneMore/Ribbon/Ribbon.xml b/OneMore/Ribbon/Ribbon.xml index 5e6d80ec2d..71319a3238 100644 --- a/OneMore/Ribbon/Ribbon.xml +++ b/OneMore/Ribbon/Ribbon.xml @@ -737,6 +737,12 @@ getLabel="GetRibbonLabel" getScreentip="GetRibbonScreentip" onAction="NameUrlsCmd"/> +