@@ -15,6 +15,7 @@ public record ConfigurationFile : DocumentationFile
15
15
private readonly IFileInfo _sourceFile ;
16
16
private readonly IDirectoryInfo _rootPath ;
17
17
private readonly BuildContext _context ;
18
+ private readonly int _depth ;
18
19
public string ? Project { get ; }
19
20
public Glob [ ] Exclude { get ; } = [ ] ;
20
21
@@ -28,12 +29,13 @@ public record ConfigurationFile : DocumentationFile
28
29
private readonly Dictionary < string , string > _substitutions = new ( StringComparer . OrdinalIgnoreCase ) ;
29
30
public IReadOnlyDictionary < string , string > Substitutions => _substitutions ;
30
31
31
- public ConfigurationFile ( IFileInfo sourceFile , IDirectoryInfo rootPath , BuildContext context )
32
+ public ConfigurationFile ( IFileInfo sourceFile , IDirectoryInfo rootPath , BuildContext context , int depth = 0 , string parentPath = "" )
32
33
: base ( sourceFile , rootPath )
33
34
{
34
35
_sourceFile = sourceFile ;
35
36
_rootPath = rootPath ;
36
37
_context = context ;
38
+ _depth = depth ;
37
39
if ( ! sourceFile . Exists )
38
40
{
39
41
Project = "unknown" ;
@@ -81,7 +83,13 @@ public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildCon
81
83
ExternalLinkHosts . Add ( host ) ;
82
84
break ;
83
85
case "toc" :
84
- var entries = ReadChildren ( entry , string . Empty ) ;
86
+ if ( depth > 1 )
87
+ {
88
+ EmitError ( $ "toc.yml files may only be linked from docset.yml", entry . Key ) ;
89
+ break ;
90
+ }
91
+
92
+ var entries = ReadChildren ( entry , parentPath ) ;
85
93
86
94
TableOfContents = entries ;
87
95
break ;
@@ -115,20 +123,19 @@ private List<ITocItem> ReadChildren(KeyValuePair<YamlNode, YamlNode> entry, stri
115
123
return entries ;
116
124
}
117
125
118
- foreach ( var tocEntry in sequence . Children . OfType < YamlMappingNode > ( ) )
119
- {
120
- var tocItem = ReadChild ( tocEntry , parentPath ) ;
121
- if ( tocItem is not null )
122
- entries . Add ( tocItem ) ;
123
- }
126
+ entries . AddRange (
127
+ sequence . Children . OfType < YamlMappingNode > ( )
128
+ . SelectMany ( tocEntry => ReadChild ( tocEntry , parentPath ) ?? [ ] )
129
+ ) ;
124
130
125
131
return entries ;
126
132
}
127
133
128
- private ITocItem ? ReadChild ( YamlMappingNode tocEntry , string parentPath )
134
+ private IEnumerable < ITocItem > ? ReadChild ( YamlMappingNode tocEntry , string parentPath )
129
135
{
130
136
string ? file = null ;
131
137
string ? folder = null ;
138
+ ConfigurationFile ? toc = null ;
132
139
var fileFound = false ;
133
140
var folderFound = false ;
134
141
IReadOnlyCollection < ITocItem > ? children = null ;
@@ -137,6 +144,9 @@ private List<ITocItem> ReadChildren(KeyValuePair<YamlNode, YamlNode> entry, stri
137
144
var key = ( ( YamlScalarNode ) entry . Key ) . Value ;
138
145
switch ( key )
139
146
{
147
+ case "toc" :
148
+ toc = ReadNestedToc ( entry , parentPath , out fileFound ) ;
149
+ break ;
140
150
case "file" :
141
151
file = ReadFile ( entry , parentPath , out fileFound ) ;
142
152
break ;
@@ -150,15 +160,23 @@ private List<ITocItem> ReadChildren(KeyValuePair<YamlNode, YamlNode> entry, stri
150
160
}
151
161
}
152
162
163
+ if ( toc is not null )
164
+ {
165
+ foreach ( var f in toc . Files )
166
+ Files . Add ( f ) ;
167
+
168
+ return [ new FolderReference ( $ "{ parentPath } ". TrimStart ( '/' ) , folderFound , toc . TableOfContents ) ] ;
169
+ }
170
+
153
171
if ( file is not null )
154
- return new TocFile ( $ "{ parentPath } /{ file } ". TrimStart ( '/' ) , fileFound , children ?? [ ] ) ;
172
+ return [ new FileReference ( $ "{ parentPath } /{ file } ". TrimStart ( '/' ) , fileFound , children ?? [ ] ) ] ;
155
173
156
174
if ( folder is not null )
157
175
{
158
176
if ( children is null )
159
177
ImplicitFolders . Add ( parentPath . TrimStart ( '/' ) ) ;
160
178
161
- return new TocFolder ( $ "{ parentPath } ". TrimStart ( '/' ) , folderFound , children ?? [ ] ) ;
179
+ return [ new FolderReference ( $ "{ parentPath } ". TrimStart ( '/' ) , folderFound , children ?? [ ] ) ] ;
162
180
}
163
181
164
182
return null ;
@@ -226,6 +244,29 @@ private Dictionary<string, string> ReadDictionary(KeyValuePair<YamlNode, YamlNod
226
244
return file ;
227
245
}
228
246
247
+ private ConfigurationFile ? ReadNestedToc ( KeyValuePair < YamlNode , YamlNode > entry , string parentPath , out bool found )
248
+ {
249
+ found = false ;
250
+ var tocPath = ReadString ( entry ) ;
251
+ if ( tocPath is null )
252
+ {
253
+ EmitError ( $ "Empty toc: reference", entry . Key ) ;
254
+ return null ;
255
+ }
256
+
257
+ var rootPath = _context . ReadFileSystem . DirectoryInfo . New ( Path . Combine ( _rootPath . FullName , tocPath ) ) ;
258
+ var path = Path . Combine ( rootPath . FullName , "toc.yml" ) ;
259
+ var source = _context . ReadFileSystem . FileInfo . New ( path ) ;
260
+ if ( ! source . Exists )
261
+ EmitError ( $ "Nested toc: '{ source . FullName } ' does not exist", entry . Key ) ;
262
+ else
263
+ found = true ;
264
+
265
+ var nestedConfiguration = new ConfigurationFile ( source , _rootPath , _context , _depth + 1 , tocPath ) ;
266
+ return nestedConfiguration ;
267
+ }
268
+
269
+
229
270
private string ? ReadString ( KeyValuePair < YamlNode , YamlNode > entry )
230
271
{
231
272
if ( entry . Value is YamlScalarNode scalar )
0 commit comments