public override void Execute(params object[] args) { using (var one = new OneNote()) { var section = one.GetSection(); var ns = one.GetNamespace(section); // find all level 1 pages not collapsed and immediately followed by level 2 page var pages = from e in section.Elements(ns + "Page") let n = e.NextNode where n != null && e.Attribute("pageLevel").Value.Equals("1") && !e.Attributes("isCollapsed").Any(x => x.Value.Equals("true")) && n.NodeType == XmlNodeType.Element && ((XElement)n).Attribute("pageLevel").Value.Equals("2") select e; if (pages?.Count() > 0) { logger.WriteLine($"found {pages.Count()} expanded pages"); foreach (var page in pages) { page.Add(new XAttribute("isCollapsed", "true")); } one.UpdateHierarchy(section); } else { logger.WriteLine($"found 0 expanded pages"); } } }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #region InsertPagesTables private void InsertPagesTable(OneNote one) { var section = one.GetSection(); var sectionId = section.Attribute("ID").Value; one.CreatePage(sectionId, out var pageId); var page = one.GetPage(pageId); var ns = page.Namespace; page.Title = string.Format(Resx.InsertTocCommand_TOCSections, section.Attribute("name").Value); var container = new XElement(ns + "OEChildren"); var elements = section.Elements(ns + "Page"); foreach (var element in elements) { var text = new StringBuilder(); var level = int.Parse(element.Attribute("pageLevel").Value); while (level > 0) { text.Append(". . "); level--; } var link = one.GetHyperlink(element.Attribute("ID").Value, string.Empty); var name = element.Attribute("name").Value; text.Append($"<a href=\"{link}\">{name}</a>"); container.Add(new XElement(ns + "OE", new XElement(ns + "T", new XCData(text.ToString()) ))); } var title = page.Root.Elements(ns + "Title").FirstOrDefault(); title.AddAfterSelf(new XElement(ns + "Outline", container)); one.Update(page); // move TOC page to top of section... // get current section again after new page is created section = one.GetSection(); var entry = section.Elements(ns + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == pageId); entry.Remove(); section.AddFirst(entry); one.UpdateHierarchy(section); one.NavigateTo(pageId); }
public override void Execute(params object[] args) { using (var one = new OneNote()) { var notebook = one.GetNotebook(); if (notebook != null) { pattern = new Regex(@"^(\(\d+\)\s).+"); if (ApplyNumbering(notebook, one.GetNamespace(notebook)) > 0) { one.UpdateHierarchy(notebook); } } } }
/* * private static void SortPages(SortDialog.Sortings sorting, SortDialog.Directions direction) * { * using (var manager = new ApplicationManager()) * { * var section = manager.CurrentSection(); * var ns = section.GetNamespaceOfPrefix("one"); * * var tree = new List<PageNode>(); * var list = section.Elements(ns + "Page").ToList(); * BuildTree(tree, list, 0, 0); * } * } * * * private static int BuildTree(List<PageNode> tree, List<XElement> elements, int level, int index) * { * if (index >= elements.Count) * { * return index; * } * * var element = elements[index]; * var pageLevel = int.Parse(element.Attribute("pageLevel").Value); * * if (pageLevel < level) * { * return index; * } * * if (pageLevel > level) * { * index = BuildTree(tree, elements, pageLevel, index + 1); * } * * return index; * } */ // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Sections private void SortSections( SortDialog.Sortings sorting, SortDialog.Directions direction, bool pinNotes) { #region Notes /* * <one:Notebook name="Personal" nickname="Personal" ID=""> * <one:Section name="Notes" ID="" /> * <one:Section name="Automobiles" ID="" /> * <one:SectionGroup name="OneNote_RecycleBin" ID="" isRecycleBin="true"> * <one:Section name="Deleted Pages" ID="" isInRecycleBin="true" isDeletedPages="true" /> * </one:SectionGroup> * </one:Notebook> * * Sort top-level sections only; ignore section groups. */ #endregion Notes logger.StartClock(); using (var one = new OneNote()) { // get the current notebook with its sections var notebook = one.GetNotebook(); if (notebook == null) { return; } var ns = one.GetNamespace(notebook); var key = sorting == SortDialog.Sortings.ByName ? "name" : "lastModifiedTime"; SortSection(notebook, ns, key, direction == SortDialog.Directions.Ascending, pinNotes); //logger.WriteLine(notebook.ToString()); one.UpdateHierarchy(notebook); } logger.WriteTime(nameof(SortSections)); }
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Notebooks private void SortNotebooks(SortDialog.Sortings sorting, SortDialog.Directions direction) { #region Notes /* * <one:Notebook name="Personal" nickname="Personal" ID="" path="" /> */ #endregion Notes logger.StartClock(); using (var one = new OneNote()) { var root = one.GetNotebooks(); var ns = one.GetNamespace(root); // nickname is display name whereas name is the folder name var key = sorting == SortDialog.Sortings.ByName ? "nickname" : "lastModifiedTime"; IEnumerable <XElement> books; if (direction == SortDialog.Directions.Descending) { books = root.Elements(ns + "Notebook") .OrderByDescending(s => s.Attribute(key).Value); } else { books = root.Elements(ns + "Notebook") .OrderBy(s => s.Attribute(key).Value); } root.ReplaceNodes(books); //logger.WriteLine(root.ToString()); one.UpdateHierarchy(root); } logger.WriteTime(nameof(SortNotebooks)); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #region InsertSectionsTable private void InsertSectionsTable(OneNote one, bool includePages) { var section = one.GetSection(); var sectionId = section.Attribute("ID").Value; one.CreatePage(sectionId, out var pageId); var page = one.GetPage(pageId); var ns = page.Namespace; var scope = includePages ? OneNote.Scope.Pages : OneNote.Scope.Sections; var notebook = one.GetNotebook(scope); page.Title = string.Format(Resx.InsertTocCommand_TOCNotebook, notebook.Attribute("name").Value); var container = new XElement(ns + "OEChildren"); BuildSectionTable(one, ns, container, notebook.Elements(), includePages, 1); var title = page.Root.Elements(ns + "Title").FirstOrDefault(); title.AddAfterSelf(new XElement(ns + "Outline", container)); one.Update(page); // move TOC page to top of section... // get current section again after new page is created section = one.GetSection(); var entry = section.Elements(ns + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == pageId); entry.Remove(); section.AddFirst(entry); one.UpdateHierarchy(section); one.NavigateTo(pageId); }
private void MovePages(string sectionId, OneNote one) { var sections = new Dictionary <string, XElement>(); var section = one.GetSection(sectionId); var ns = one.GetNamespace(section); var updated = false; using (var progress = new ProgressDialog()) { progress.SetMaximum(pageIds.Count); progress.Show(owner); foreach (var pageId in pageIds) { // find the section that currently owns the page var parentId = one.GetParent(pageId); if (parentId == sectionId) { continue; } // load the owning section XElement parent; if (sections.ContainsKey(parentId)) { parent = sections[parentId]; } else { parent = one.GetSection(parentId); sections.Add(parentId, parent); } // get the Page reference within the owing section var element = parent.Elements(ns + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == pageId); if (element != null) { progress.SetMessage(element.Attribute("name").Value); // remove page from current owner element.Remove(); // remove misc attributes; OneNote will recreate them element.Attributes() .Where(a => a.Name != "ID" && a.Name != "name") .Remove(); // add page to target section section.Add(element); updated = true; } progress.Increment(); } } // updated at least one if (updated) { // update each source section foreach (var s in sections.Values) { one.UpdateHierarchy(s); } sections.Clear(); // update target section one.UpdateHierarchy(section); } }
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Pages private void SortPages(SortDialog.Sortings sorting, SortDialog.Directions direction) { #region Notes /* * <one:Page ID="" name="Notes" pageLevel="1" /> * * Pages within a section are stored as a flat list of elements with * indented pages indicated by pageLevel only - they are not recursive children. * So the code below must group child pages with their parent so all parents * can be sorted correctly. Children are not sorted. */ #endregion Notes logger.StartClock(); using (var one = new OneNote()) { var root = one.GetSection(); var ns = one.GetNamespace(root); var pages = new List <PageNode>(); foreach (var child in root.Elements(ns + "Page")) { if (child.Attribute("pageLevel").Value == "1") { // found the next parent page pages.Add(new PageNode(child)); } else { // grouping child pages with the top parent pages[pages.Count - 1].Children.Add(child); } } if (direction == SortDialog.Directions.Descending) { if (sorting == SortDialog.Sortings.ByName) { pages = pages.OrderByDescending( p => EmojiDialog.RemoveEmojis(p.Page.Attribute("name").Value)).ToList(); } else { var key = sorting == SortDialog.Sortings.ByCreated ? "dateTime" : "lastModifiedTime"; pages = pages.OrderByDescending( p => p.Page.Attribute(key).Value).ToList(); } } else { if (sorting == SortDialog.Sortings.ByName) { pages = pages.OrderBy( p => EmojiDialog.RemoveEmojis(p.Page.Attribute("name").Value)).ToList(); } else { var key = sorting == SortDialog.Sortings.ByCreated ? "dateTime" : "lastModifiedTime"; pages = pages.OrderBy( p => p.Page.Attribute(key).Value).ToList(); } } root.RemoveNodes(); // recreate flat list foreach (var page in pages) { root.Add(page.Page); foreach (var child in page.Children) { root.Add(child); } } //logger.WriteLine(root.ToString()); one.UpdateHierarchy(root); } logger.WriteTime(nameof(SortPages)); }