4
4
5
5
namespace Odotocodot . OneNote . Linq
6
6
{
7
+ /// <summary>
8
+ /// A static class containing extension methods for the <see cref="IOneNoteItem"/> object.
9
+ /// </summary>
7
10
public static class IOneNoteItemExtensions
8
11
{
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><<see cref="IOneNoteItem"/>> 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 )
11
20
{
12
21
var stack = new Stack < IOneNoteItem > ( ) ;
13
- stack . Push ( item ) ;
22
+ stack . Push ( source ) ;
14
23
while ( stack . Count > 0 )
15
24
{
16
25
var current = stack . Pop ( ) ;
17
26
18
- if ( predicate ( current ) )
19
- yield return current ;
27
+ yield return current ;
20
28
21
29
foreach ( var child in current . Children )
22
30
stack . Push ( child ) ;
23
31
}
24
32
}
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><<see cref="IOneNoteItem"/>> 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 )
26
45
{
27
46
var stack = new Stack < IOneNoteItem > ( ) ;
28
- stack . Push ( item ) ;
47
+ stack . Push ( source ) ;
29
48
while ( stack . Count > 0 )
30
49
{
31
50
var current = stack . Pop ( ) ;
32
51
33
- yield return current ;
52
+ if ( predicate ( current ) )
53
+ yield return current ;
34
54
35
55
foreach ( var child in current . Children )
36
56
stack . Push ( child ) ;
37
57
}
38
58
}
39
59
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 ) ) ;
44
67
45
68
/// <inheritdoc cref="OneNoteParser.OpenInOneNote(Microsoft.Office.Interop.OneNote.IApplication, IOneNoteItem)"/>
46
69
public static void OpenInOneNote ( this IOneNoteItem item ) => OneNoteApplication . OpenInOneNote ( item ) ;
@@ -51,11 +74,18 @@ public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem>
51
74
/// <inheritdoc cref="OneNoteParser.GetPageContent(Microsoft.Office.Interop.OneNote.IApplication, OneNotePage)"/>
52
75
public static string GetPageContent ( this OneNotePage page ) => OneNoteApplication . GetPageContent ( page ) ;
53
76
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"/>
59
89
public static bool IsInRecycleBin ( this IOneNoteItem item ) => item switch
60
90
{
61
91
OneNoteSectionGroup sectionGroup => sectionGroup . IsRecycleBin ,
@@ -64,22 +94,31 @@ public static IEnumerable<IOneNoteItem> Traverse(this IEnumerable<IOneNoteItem>
64
94
_ => false ,
65
95
} ;
66
96
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>
67
105
public static bool GetRecycleBin ( this OneNoteNotebook notebook , out OneNoteSectionGroup sectionGroup )
68
106
{
69
107
sectionGroup = notebook . SectionGroups . FirstOrDefault ( sg => sg . IsRecycleBin ) ;
70
108
return sectionGroup != null ;
71
109
}
72
110
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><<see cref="OneNotePage"/>> 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 ) ;
73
119
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 ) ;
84
123
}
85
124
}
0 commit comments