Skip to content

Commit

Permalink
dynamically populate and correct page hyperlinks before copying
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Aug 13, 2024
1 parent 68170f4 commit f7ff0d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
13 changes: 12 additions & 1 deletion OneMoreCalendar/CalendarPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ namespace OneMoreCalendar
/// <summary>
///
/// </summary>
internal class CalendarPages : List<CalendarPage> { }
internal class CalendarPages : List<CalendarPage>
{
public CalendarPages()
: base()
{
}

public CalendarPages(IEnumerable<CalendarPage> pages)
: base(pages)
{
}
}


/// <summary>
Expand Down
38 changes: 28 additions & 10 deletions OneMoreCalendar/MonthView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,26 +644,44 @@ private async void ClickCopyPageButton(object sender, EventArgs e)
{
if (((MoreButton)sender).Tag is CalendarDay day)
{
// if we have at least one Hyperlink then we've been here before!
if (!day.Pages.Exists(p => p.Hyperlink is not null))
// fill and correct hyperlinks...

var candidates = day.Pages
.Where(p => p.Hyperlink is null)
.Select(p => p);

if (candidates.Any())
{
var empties = new CalendarPages(candidates);

var one = new OneNoteProvider();
await one.GetPageLinks(day.Pages);
await one.GetPageLinks(empties);

// hyperlinks are returned from the OneNote API backwards from the expected
// format so this matches the two parts that needs to be swapped
var regex = new Regex(@"onenote:(#Boxing&.+?&end)&base-path=(https:.+)");
// hyperlinks may be returned from the OneNote API backwards from the
// expected format so this matches the two parts that needs to be swapped
var regex = new Regex(@"onenote:(#.+?&end)&base-path=(https:.+)");

foreach (var page in day.Pages)
foreach (var page in empties)
{
var match = regex.Match(page.Hyperlink);
if (match.Success)
if (page.Hyperlink.StartsWith("onenote:https:"))
{
page.Hyperlink = $"onenote:{match.Groups[2].Value}{match.Groups[1].Value}";
// hyperlink is correct, just strip onenote: part
page.Hyperlink = page.Hyperlink.Substring(8);
}
else
{
var match = regex.Match(page.Hyperlink);
if (match.Success)
{
// hyperlink is reversed, so correct it
page.Hyperlink = $"onenote:{match.Groups[2].Value}{match.Groups[1].Value}";
}
}
}
}

// copy...

var pages = day.Pages.Where(p => p.Hyperlink is not null).ToList();
if (pages.Any())
{
Expand Down

0 comments on commit f7ff0d6

Please sign in to comment.