Skip to content

Commit a78d321

Browse files
authored
Allow _docset.yml and _toc.yml as configuration filenames (#361)
* Allow _docset.yml and _toc.yml as configuration filenames * revert enhanced code block parser changes * fix slow lookup and randomly use underscore docset.yml in tests * target known locations first before initiating a folder scan
1 parent c78f7d9 commit a78d321

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/Elastic.Markdown/BuildContext.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,42 @@ public BuildContext(IFileSystem readFileSystem, IFileSystem writeFileSystem, str
4949
var rootFolder = !string.IsNullOrWhiteSpace(source)
5050
? ReadFileSystem.DirectoryInfo.New(source)
5151
: ReadFileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName));
52-
SourcePath = FindDocsFolderFromRoot(rootFolder);
52+
53+
(SourcePath, ConfigurationPath) = FindDocsFolderFromRoot(rootFolder);
5354

5455
OutputPath = !string.IsNullOrWhiteSpace(output)
5556
? WriteFileSystem.DirectoryInfo.New(output)
5657
: WriteFileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName, ".artifacts/docs/html"));
5758

58-
ConfigurationPath =
59-
ReadFileSystem.FileInfo.New(Path.Combine(SourcePath.FullName, "docset.yml"));
60-
6159
if (ConfigurationPath.FullName != SourcePath.FullName)
6260
SourcePath = ConfigurationPath.Directory!;
6361

6462
Git = GitCheckoutInformation.Create(ReadFileSystem);
6563
}
6664

67-
private IDirectoryInfo FindDocsFolderFromRoot(IDirectoryInfo rootPath)
65+
private (IDirectoryInfo, IFileInfo) FindDocsFolderFromRoot(IDirectoryInfo rootPath)
6866
{
69-
if (rootPath.Exists &&
70-
ReadFileSystem.File.Exists(Path.Combine(rootPath.FullName, "docset.yml")))
71-
return rootPath;
72-
73-
var docsFolder = rootPath.EnumerateFiles("docset.yml", SearchOption.AllDirectories).FirstOrDefault();
74-
return docsFolder?.Directory ?? throw new Exception($"Can not locate docset.yml file in '{rootPath}'");
67+
string[] files = ["docset.yml", "_docset.yml"];
68+
string[] knownFolders = [rootPath.FullName, Path.Combine(rootPath.FullName, "docs")];
69+
var mostLikelyTargets =
70+
from file in files
71+
from folder in knownFolders
72+
select Path.Combine(folder, file);
73+
74+
var knownConfigPath = mostLikelyTargets.FirstOrDefault(ReadFileSystem.File.Exists);
75+
var configurationPath = knownConfigPath is null ? null : ReadFileSystem.FileInfo.New(knownConfigPath);
76+
if (configurationPath is not null)
77+
return (configurationPath.Directory!, configurationPath);
78+
79+
configurationPath = rootPath
80+
.EnumerateFiles("*docset.yml", SearchOption.AllDirectories)
81+
.FirstOrDefault()
82+
?? throw new Exception($"Can not locate docset.yml file in '{rootPath}'");
83+
84+
var docsFolder = configurationPath.Directory
85+
?? throw new Exception($"Can not locate docset.yml file in '{rootPath}'");
86+
87+
return (docsFolder, configurationPath);
7588
}
7689

7790
}

src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,17 @@ private Dictionary<string, string> ReadDictionary(KeyValuePair<YamlNode, YamlNod
257257
var rootPath = _context.ReadFileSystem.DirectoryInfo.New(Path.Combine(_rootPath.FullName, tocPath));
258258
var path = Path.Combine(rootPath.FullName, "toc.yml");
259259
var source = _context.ReadFileSystem.FileInfo.New(path);
260+
261+
var errorMessage = $"Nested toc: '{source.Directory}' directory has no toc.yml or _toc.yml file";
262+
263+
if (!source.Exists)
264+
{
265+
path = Path.Combine(rootPath.FullName, "_toc.yml");
266+
source = _context.ReadFileSystem.FileInfo.New(path);
267+
}
268+
260269
if (!source.Exists)
261-
EmitError($"Nested toc: '{source.FullName}' does not exist", entry.Key);
270+
EmitError(errorMessage, entry.Key);
262271
else
263272
found = true;
264273

tests/authoring/Framework/Setup.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace authoring
66

77

8+
open System
89
open System.Collections.Generic
910
open System.IO
1011
open System.IO.Abstractions.TestingHelpers
@@ -53,7 +54,8 @@ type Setup =
5354
)
5455
| _ -> ()
5556

56-
fileSystem.AddFile(Path.Combine(root.FullName, "docset.yml"), MockFileData(yaml.ToString()));
57+
let name = if Random().Next(0, 10) % 2 = 0 then "_docset.yml" else "docset.yml"
58+
fileSystem.AddFile(Path.Combine(root.FullName, name), MockFileData(yaml.ToString()));
5759

5860
static member Generator (files: TestFile seq) : Task<GeneratorResults> =
5961

0 commit comments

Comments
 (0)