Skip to content

Commit

Permalink
Add command to replace URs with addresses (#1714)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored Dec 22, 2024
1 parent b9f4226 commit 37808b0
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 16 deletions.
5 changes: 5 additions & 0 deletions OneMore/AddInCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,11 @@ public async Task TrimLeadingCmd(IRibbonControl control)
=> await factory.Run<TrimCommand>(true);


[Command("ribUnnameUrlsButton_Label", Keys.None, "ribReferencesMenu")]
public async Task UnnameUrlsCmd(IRibbonControl control)
=> await factory.Run<UnnameUrlsCommand>();


[Command("ribUpdatePageTimeButton_Label", Keys.None, "ribPageMenu")]
public async Task UpdatePageTimeCmd(IRibbonControl control)
=> await factory.Run<UpdatePageTimeCommand>(true);
Expand Down
91 changes: 75 additions & 16 deletions OneMore/Commands/References/NameUrlsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//************************************************************************************************
// Copyright © 2020 Steven M Cohn. All rights reserved.
// Copyright © 2020 Steven M Cohn. All rights reserved.
//************************************************************************************************

namespace River.OneMoreAddIn.Commands
Expand All @@ -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
{

Expand All @@ -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<bool> 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<XElement> GetCandiateElements(Page page)
{
List<XElement> elements = null;
List<XElement> elements;

// OneNote XML will insert CR prior to 'href' in the CDATA
var regex = new Regex(@"<a\s+href=", RegexOptions.Compiled);

var selections = page.Root.Descendants(page.Namespace + "T")
.Where(e =>
e.Attributes("selected").Any(a => a.Value.Equals("all")));
var range = new SelectionRange(page);
range.GetSelection();

if ((selections.Count() == 1) &&
(selections.First().DescendantNodes().OfType<XCData>().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<XCData>()
// entire page
elements = page.Root
.DescendantNodes().OfType<XCData>()
.Where(c => regex.IsMatch(c.Value))
.Select(e => e.Parent)
.ToList();
}
else
{
// selected range so affect only within that
elements = page.Root.DescendantNodes().OfType<XCData>()
// only selections
elements = page.Root
.DescendantNodes().OfType<XCData>()
.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<bool> NameUrls(Page page)
{
var elements = GetCandiateElements(page);

// parallelize internet access for all hyperlinks on page...

int count = 0;
Expand Down Expand Up @@ -97,7 +156,7 @@ private async Task<int> ReplaceUrlText(XElement element)

var wrapper = cdata.GetWrapper();
var anchor = wrapper.Element("a");
if (anchor == null)
if (anchor is null)
{
return 0;
}
Expand Down Expand Up @@ -168,7 +227,7 @@ private async Task<string> 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
Expand Down
18 changes: 18 additions & 0 deletions OneMore/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.ar-SA.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4243,6 +4243,14 @@ ISO-code then comma then language name</comment>
<value>اقتطاع المسافة البيضاء البادئة من النص المحدد</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>استبدل عناوين URL بعنوان الويب</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>استبدل عناوين URL بعنوان الويب الأساسي</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>تحديث وقت الصفحة</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.de-DE.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4239,6 +4239,14 @@ Nach der letzten Gruppe</value>
<value>Entfernt führende Leerzeichen aus ausgewähltem Text</value>
<comment>ribbon edit</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>Ersetzen Sie URLs durch Webadressen</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>Ersetzen Sie URL-Titel durch einfache Webadressen</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>Aktualisieren Sie die Seitenzeit</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.es-ES.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4243,6 +4243,14 @@ Después del último grupo</value>
<value>Recortar los espacios en blanco iniciales del texto seleccionado</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>Reemplazar URL con dirección web</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>Reemplazar títulos de URL con direcciones web básicas</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>Hora de actualización de la página</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4242,6 +4242,14 @@ Après le dernier groupe</value>
<value>Couper les espaces blancs de début du texte sélectionné</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>Remplacer les URL par une adresse Web</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>Remplacer les titres d'URL par une adresse Web de base</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>Mettre à jour l'heure de la page</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.he-IL.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4242,6 +4242,14 @@ ISO-code then comma then language name</comment>
<value>חתוך את הרווח הלבן המוביל מהטקסט שנבחר</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>החלף כתובות URL בכתובת אינטרנט</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>החלף את כותרות הכתובות בכתובת אינטרנט בסיסית</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>עדכן את זמן העמוד</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.ja-JP.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4247,6 +4247,14 @@ ISO-code then comma then language name</comment>
<value>選択したテキストから先頭のホワイトスペースを削除する</value>
<comment>ribbon edit</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>URL を Web アドレスに置き換えます</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>URL タイトルを基本的な Web アドレスに置き換えます</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>ページ時間の更新</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.nl-NL.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4243,6 +4243,14 @@ Na de laatste groep</value>
<value>Snijd de witruimte voor uit de geselecteerde tekst</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>Vervang URL's door een webadres</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>Vervang URL-titels door een standaard webadres</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>Paginatijd bijwerken</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.pl-PL.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4244,6 +4244,14 @@ Po ostatniej grupie</value>
<value>Wykończenia prowadzące białe znaki z wybranego tekstu</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>Zastąp adresy URL adresem internetowym</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>Zastąp tytuły adresów URL podstawowym adresem internetowym</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>Zaktualizuj czas strony</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.pt-BR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4243,6 +4243,14 @@ Depois do último grupo</value>
<value>Cortar o espaço em branco inicial do texto selecionado</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>Substitua URLs por endereço da web</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>Substitua os títulos de URL por endereços da Web básicos</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>Atualizar tempo da página</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4245,6 +4245,14 @@ After last group</value>
<value>Trim leading whitespace from selected text</value>
<comment>ribbon edit</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>Replace URLs with Web Address</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>Replace URL titles with basic Web address</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>Update Page Time</value>
<comment>page menu</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4236,6 +4236,14 @@ ISO-code then comma then language name</comment>
<value>修剪所选文本中的前导空白</value>
<comment>ribbon item tootp</comment>
</data>
<data name="ribUnnameUrlsButton_Label" xml:space="preserve">
<value>将 URL 替换为网址</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUnnameUrlsButton_Screentip" xml:space="preserve">
<value>将 URL 标题替换为基本网址</value>
<comment>ribbon reference</comment>
</data>
<data name="ribUpdatePageTimeButton_Label" xml:space="preserve">
<value>更新页面时间</value>
<comment>page menu</comment>
Expand Down
6 changes: 6 additions & 0 deletions OneMore/Ribbon/Ribbon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@
getLabel="GetRibbonLabel"
getScreentip="GetRibbonScreentip"
onAction="NameUrlsCmd"/>
<button
id="ribUnnameUrlsButton"
imageMso="HyperlinkEdit"
getLabel="GetRibbonLabel"
getScreentip="GetRibbonScreentip"
onAction="UnnameUrlsCmd"/>
<button
id="ribGetImagesButton"
imageMso="PictureReset"
Expand Down

0 comments on commit 37808b0

Please sign in to comment.