public override async Task Execute(params object[] args) { string whatText; string withText; bool matchCase; bool useRegex; using (var one = new OneNote(out var page, out _)) { var text = page.GetSelectedText(); using (var dialog = new SearchAndReplaceDialog()) { if (text.Length > 0) { dialog.WhatText = text; } if (dialog.ShowDialog(owner) != DialogResult.OK) { return; } whatText = dialog.WhatText; withText = dialog.WithText; matchCase = dialog.MatchCase; useRegex = dialog.UseRegex; } // let user insert a newline char withText = withText.Replace("\\n", "\n"); var editor = new SearchAndReplaceEditor(whatText, withText, useRegex: useRegex, caseSensitive: matchCase ); var count = editor.SearchAndReplace(page); if (count > 0) { logger.WriteLine($"found {count} matches"); await one.Update(page); SaveSettings(whatText, withText, matchCase, useRegex); } else { logger.WriteLine("no matches found"); } } }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Invoked by the ProgressDialog OnShown callback private async Task Execute(UI.ProgressDialog progress, CancellationToken token) { logger.Start(); logger.StartClock(); using (one = new OneNote()) { if (page == null) { page = one.GetPage(); ns = page.Namespace; } PageNamespace.Set(ns); var title = Unstamp(page.Title); string startId = string.Empty; switch (scope) { case OneNote.Scope.Sections: startId = one.CurrentNotebookId; break; case OneNote.Scope.Pages: startId = one.CurrentSectionId; break; } logger.WriteLine($"searching for '{title}'"); var results = one.Search(startId, title, unindexed); if (token.IsCancellationRequested) { logger.WriteLine("cancelled"); return; } //logger.WriteLine(results); var referals = FlattenPages(results, page.PageId); var total = referals.Count(); if (total == 0) { UIHelper.ShowInfo(Resx.LinkReferencesCommand_noref); return; } // initialize search-and-replace editor... var whatText = $@"\b{SearchAndReplaceEditor.EscapeEscapes(title)}\b"; var pageLink = one.GetHyperlink(page.PageId, string.Empty); var withElement = new XElement("A", new XAttribute("href", pageLink), page.Title ); var editor = new SearchAndReplaceEditor(whatText, withElement, useRegex: true, caseSensitive: false ); // process pages... progress.SetMaximum(total); progress.SetMessage($"Linking for {total} pages"); var updates = 0; foreach (var referal in referals) { progress.Increment(); progress.SetMessage(referal.Attribute(NameAttr).Value); var refpage = one.GetPage(referal.Attribute("ID").Value, OneNote.PageDetail.Basic); logger.WriteLine($"searching for '{whatText}' on {refpage.Title}"); var count = editor.SearchAndReplace(refpage); if (count > 0) { await one.Update(refpage); referal.SetAttributeValue(LinkedAttr, "true"); referal.SetAttributeValue(SynopsisAttr, GetSynopsis(refpage)); updates++; } else { logger.WriteLine($"search not found on {referal.Attribute(NameAttr).Value}"); } if (token.IsCancellationRequested) { logger.WriteLine("cancelled"); break; } } if (updates > 0) { // even if cancellation is request, must update page with referals that were // modified, otherwise, there will be referal pages that link to this page // without this page referring back! AppendReferalBlock(page, referals); await one.Update(page); } } logger.WriteTime("linking complete"); logger.End(); }
public override async Task Execute(params object[] args) { string whatText; string withText; bool matchCase; using (var one = new OneNote(out var page, out var ns)) { var text = page.GetSelectedText(); using (var dialog = new SearchAndReplaceDialog()) { if (text.Length > 0) { dialog.WhatText = text; } if (dialog.ShowDialog(owner) != DialogResult.OK) { return; } whatText = dialog.WhatText; withText = dialog.WithText; matchCase = dialog.MatchCase; } // let user insert a newline char withText = withText.Replace("\\n", "\n"); var cursor = page.GetTextCursor(); if (cursor != null) { MergeRuns(cursor); } IEnumerable <XElement> elements; if (cursor != null) { elements = page.Root.Elements(ns + "Outline") .Descendants(ns + "T"); } else { elements = page.Root.Elements(ns + "Outline") .Descendants(ns + "T") .Where(e => e.Attribute("selected")?.Value == "all"); } if (elements.Any()) { int count = 0; var editor = new SearchAndReplaceEditor(whatText, withText, matchCase); foreach (var element in elements) { count += editor.SearchAndReplace(element); } if (count > 0) { PatchEndingBreaks(page.Root, ns); logger.WriteLine($"found {count} matches"); await one.Update(page); } else { logger.WriteLine("no matches found"); } } } }