Skip to content

Commit baf9d6a

Browse files
committed
Update documentation
1 parent cd0a084 commit baf9d6a

File tree

3 files changed

+107
-54
lines changed

3 files changed

+107
-54
lines changed

Odotocodot.OneNote.Linq/IOneNoteItemExtensions.cs

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,66 @@
44

55
namespace Odotocodot.OneNote.Linq
66
{
7+
/// <summary>
8+
/// A static class containing extension methods for the <see cref="IOneNoteItem"/> object.
9+
/// </summary>
710
public static class IOneNoteItemExtensions
811
{
9-
//Depth first traversal
10-
public static IEnumerable<IOneNoteItem> Traverse(this IOneNoteItem item, Func<IOneNoteItem, bool> predicate)
12+
/// <summary>
13+
/// Returns a flattened collection of OneNote items, that contains the children of every OneNote item from the <paramref name="source"/>.
14+
/// </summary>
15+
/// <param name="source">The source OneNote item.</param>
16+
/// <returns>An <see cref="IEnumerable{T}">IEnumerable</see>&lt;<see cref="IOneNoteItem"/>&gt; containing the
17+
/// child items of the <paramref name="source"/>.</returns>
18+
/// <remarks>This method uses a non recursive depth first traversal algorithm.</remarks>
19+
public static IEnumerable<IOneNoteItem> Traverse(this IOneNoteItem source)
1120
{
1221
var stack = new Stack<IOneNoteItem>();
13-
stack.Push(item);
22+
stack.Push(source);
1423
while (stack.Count > 0)
1524
{
1625
var current = stack.Pop();
1726

18-
if (predicate(current))
19-
yield return current;
27+
yield return current;
2028

2129
foreach (var child in current.Children)
2230
stack.Push(child);
2331
}
2432
}
25-
public static IEnumerable<IOneNoteItem> Traverse(this IOneNoteItem item)
33+
34+
/// <summary>
35+
/// Returns a filtered flattened collection of OneNote items, that contains the children of every OneNote item
36+
/// from the <paramref name="source"/>.<br/>
37+
/// Only items that successfully pass the <paramref name="predicate"/> are returned.
38+
/// </summary>
39+
/// <param name="source"><inheritdoc cref="Traverse(IOneNoteItem)" path="/param[@name='source']"/></param>
40+
/// <param name="predicate">A function to test each item for a condition.</param>
41+
/// <returns>An <see cref="IEnumerable{T}">IEnumerable</see>&lt;<see cref="IOneNoteItem"/>&gt; containing the
42+
/// child items of the <paramref name="source"/> that pass the <paramref name="predicate"/>.</returns>
43+
/// <remarks><inheritdoc cref="Traverse(IOneNoteItem)" path="/remarks"/></remarks>
44+
public static IEnumerable<IOneNoteItem> Traverse(this IOneNoteItem source, Func<IOneNoteItem, bool> predicate)
2645
{
2746
var stack = new Stack<IOneNoteItem>();
28-
stack.Push(item);
47+
stack.Push(source);
2948
while (stack.Count > 0)
3049
{
3150
var current = stack.Pop();
3251

33-
yield return current;
52+
if (predicate(current))
53+
yield return current;
3454

3555
foreach (var child in current.Children)
3656
stack.Push(child);
3757
}
3858
}
3959

40-
public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem> items, Func<IOneNoteItem, bool> predicate)
41-
=> items.SelectMany(item => item.Traverse(predicate));
42-
public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem> items)
43-
=> items.SelectMany(item => item.Traverse());
60+
/// <inheritdoc cref="Traverse(IOneNoteItem)"/>
61+
public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem> source)
62+
=> source.SelectMany(item => item.Traverse());
63+
64+
/// <inheritdoc cref="Traverse(IOneNoteItem,Func{IOneNoteItem, bool})"/>
65+
public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem> source, Func<IOneNoteItem, bool> predicate)
66+
=> source.SelectMany(item => item.Traverse(predicate));
4467

4568
/// <inheritdoc cref="OneNoteParser.OpenInOneNote(Microsoft.Office.Interop.OneNote.IApplication, IOneNoteItem)"/>
4669
public static void OpenInOneNote(this IOneNoteItem item) => OneNoteApplication.OpenInOneNote(item);
@@ -51,11 +74,18 @@ public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem>
5174
/// <inheritdoc cref="OneNoteParser.GetPageContent(Microsoft.Office.Interop.OneNote.IApplication, OneNotePage)"/>
5275
public static string GetPageContent(this OneNotePage page) => OneNoteApplication.GetPageContent(page);
5376

54-
///// <summary>
55-
///// Returns
56-
///// </summary>
57-
///// <param name="item"></param>
58-
///// <returns> <see langword="true"/> if the item is a deleted page <see cref="OneNotePage.IsInRecycleBin"/>, deleted section, recycle bin section group, or deleted pages section.</returns>
77+
/// <summary>
78+
/// Returns a value that indicates whether the <paramref name="item"/> is in or is a recycle bin.
79+
/// </summary>
80+
/// <param name="item">The OneNote item to check.</param>
81+
/// <returns><see langword="true"/> if the <paramref name="item"/> is in or is a recycle bin; otherwise, <see langword="false"/>.</returns>
82+
/// <remarks>Checks whether the <paramref name="item"/> is a recycle bin <see cref="OneNoteSectionGroup">section group</see>,
83+
/// a deleted <see cref="OneNotePage">page</see>, a deleted <see cref="OneNoteSection">section</see>, or the deleted pages
84+
/// <see cref="OneNoteSection">section</see> within a recycle bin.</remarks>
85+
/// <seealso cref="OneNoteSectionGroup.IsRecycleBin"/>
86+
/// <seealso cref="OneNoteSection.IsInRecycleBin"/>
87+
/// <seealso cref="OneNoteSection.IsDeletedPages"/>
88+
/// <seealso cref="OneNotePage.IsInRecycleBin"/>
5989
public static bool IsInRecycleBin(this IOneNoteItem item) => item switch
6090
{
6191
OneNoteSectionGroup sectionGroup => sectionGroup.IsRecycleBin,
@@ -64,22 +94,31 @@ public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem>
6494
_ => false,
6595
};
6696

97+
/// <summary>
98+
/// Get the recycle bin <see cref="OneNoteSectionGroup">section group</see> for the specified <paramref name="notebook"/> if it exists.
99+
/// </summary>
100+
/// <param name="notebook">The notebook to get the recycle bin of.</param>
101+
/// <param name="sectionGroup">When this method returns, <paramref name="sectionGroup"/> contains the recycle bin of
102+
/// the <paramref name="notebook"/> if it was found;
103+
/// otherwise, <see langword="null"/>.</param>
104+
/// <returns><see langword="true"/> if the <paramref name="notebook"/> contains a recycle bin; otherwise, <see langword="false"/>.</returns>
67105
public static bool GetRecycleBin(this OneNoteNotebook notebook, out OneNoteSectionGroup sectionGroup)
68106
{
69107
sectionGroup = notebook.SectionGroups.FirstOrDefault(sg => sg.IsRecycleBin);
70108
return sectionGroup != null;
71109
}
72110

111+
/// <summary>
112+
/// Returns a flattened collection of all the <see cref="OneNotePage">pages</see> present in the <paramref name="source"/>.
113+
/// </summary>
114+
/// <param name="source"><inheritdoc cref="Traverse(IOneNoteItem)" path="/param[@name='source']"/></param>
115+
/// <returns>An <see cref="IEnumerable{T}">IEnumerable</see>&lt;<see cref="OneNotePage"/>&gt; containing all the
116+
/// <see cref="OneNotePage">pages</see> present in the <paramref name="source"/>.</returns>
117+
public static IEnumerable<OneNotePage> GetPages(this IOneNoteItem source)
118+
=> (IEnumerable<OneNotePage>)source.Traverse(i => i is OneNotePage);
73119

74-
///// <param name="items">The collection of items to filter get pages from.</param>
75-
///// <returns>
76-
///// A flattened collection of only the pages nested in <paramref name="items"/>.
77-
///// </returns>
78-
public static IEnumerable<OneNotePage> GetPages(this IEnumerable<IOneNoteItem> items) => (IEnumerable<OneNotePage>)items.Traverse(i => i is OneNotePage);
79-
///// <param name="item">The item to filter get pages from.</param>
80-
///// <returns>
81-
///// A flattened collection of only the pages nested in <paramref name="item"/>.
82-
///// </returns>
83-
public static IEnumerable<OneNotePage> GetPages(this IOneNoteItem item) => (IEnumerable<OneNotePage>)item.Traverse(i => i is OneNotePage);
120+
/// <inheritdoc cref="GetPages(IOneNoteItem)"/>
121+
public static IEnumerable<OneNotePage> GetPages(this IEnumerable<IOneNoteItem> source)
122+
=> (IEnumerable<OneNotePage>)source.Traverse(i => i is OneNotePage);
84123
}
85124
}

Odotocodot.OneNote.Linq/OneNoteApplication.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.Runtime.InteropServices;
54
using System.Threading;
65
using Microsoft.Office.Interop.OneNote;
76

87
namespace Odotocodot.OneNote.Linq
98
{
10-
//wrapper around com object for easy release of com object
9+
/// <summary>
10+
/// A static wrapper class around the <see cref="Application"/> class, allowing for easy acquirement and
11+
/// release of a OneNote COM object, whilst avoiding duplicate instances. In addition to exposing the
12+
/// <see href="https://learn.microsoft.com/en-us/office/client-developer/onenote/application-interface-onenote">OneNote's API</see>
13+
/// </summary>
14+
/// <remarks>A <see cref="Application">OneNote COM object</see> is required to access any of the OneNote API.</remarks>
1115
public static class OneNoteApplication
1216
{
1317
private static Application oneNote;
1418
private static bool hasComInstance = false;
19+
/// <summary>
20+
/// Indicates whether the class has a usable <see cref="Application">COM instance</see>.
21+
/// </summary>
22+
/// <remarks>When <see langword="true"/> a OneNote process should be visible in the Task Manager.</remarks>
23+
/// <seealso cref="Init"/>
24+
/// <seealso cref="ReleaseComInstance"/>
1525
public static bool HasComInstance => hasComInstance;
26+
27+
/// <summary>
28+
/// Initialises the static class by acquiring a <see cref="Application">OneNote COM object</see>.
29+
/// </summary>
30+
/// <exception cref="COMException">Thrown if an error occurred when trying to get the
31+
/// <see cref="Application">OneNote COM object</see> or the number of attempts in doing
32+
/// so exceeded the limit.</exception>
33+
/// <seealso cref="HasComInstance"/>
34+
/// <seealso cref="ReleaseComInstance"/>
1635
public static void Init()
1736
{
1837
int attempt = 0;
@@ -60,17 +79,6 @@ public static string GetPageContent(OneNotePage page)
6079
return OneNoteParser.GetPageContent(oneNote, page);
6180
}
6281

63-
public static IEnumerable<IOneNoteItem> Traverse()
64-
{
65-
Init();
66-
return GetNotebooks().Traverse();
67-
}
68-
public static IEnumerable<IOneNoteItem> Traverse(Func<IOneNoteItem, bool> predicate)
69-
{
70-
Init();
71-
return GetNotebooks().Traverse(predicate);
72-
}
73-
7482
/// <inheritdoc cref="OneNoteParser.FindPages(IApplication, string)"/>
7583
public static IEnumerable<OneNotePage> FindPages(string search)
7684
{
@@ -136,6 +144,11 @@ public static void CreateNotebook(string notebookName)
136144
OneNoteParser.CreateNotebook(oneNote, notebookName, true);
137145
}
138146

147+
/// <summary>
148+
/// Releases the <see cref="Application">OneNote COM object</see> freeing memory.
149+
/// </summary>
150+
/// <seealso cref="Init"/>
151+
/// <seealso cref="HasComInstance"/>
139152
public static void ReleaseComInstance()
140153
{
141154
if (hasComInstance)

0 commit comments

Comments
 (0)