-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs-generator tool to generate random docsets (#96)
- Loading branch information
Showing
13 changed files
with
385 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,4 @@ if [[ $nErrors -eq 0 ]]; then | |
exit 0 | ||
else | ||
exit 1 | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Bogus; | ||
|
||
namespace Documentation.Generator.Domain; | ||
|
||
public record Determinism | ||
{ | ||
public Determinism(int? seedFileSystem, int? seedContent) | ||
{ | ||
var randomizer = new Randomizer(); | ||
SeedFileSystem = seedFileSystem ?? randomizer.Int(1, int.MaxValue); | ||
SeedContent = seedContent ?? randomizer.Int(1, int.MaxValue); | ||
FileSystem = new Randomizer(SeedFileSystem); | ||
Contents = new Randomizer(SeedContent); | ||
|
||
SectionProbability = Contents.Float(0.001f, Contents.Float(0.1f)); | ||
FileProbability = FileSystem.Float(0.001f, Contents.Float(0.1f)); | ||
} | ||
|
||
public int SeedFileSystem { get; } | ||
public int SeedContent { get; } | ||
|
||
|
||
public Randomizer FileSystem { get; } | ||
public Randomizer Contents { get; } | ||
|
||
public float SectionProbability { get; } | ||
public float FileProbability { get; } | ||
|
||
public static Determinism Random { get; set; } = new(null, null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Slugify; | ||
using Soenneker.Utils.AutoBogus; | ||
|
||
namespace Documentation.Generator.Domain; | ||
|
||
public static class Generators | ||
{ | ||
public static AutoFaker<FolderName> FolderName { get; } = new(); | ||
public static AutoFaker<Section> Section { get; } = new(); | ||
public static AutoFaker<MarkdownFile> File { get; } = new(); | ||
public static SlugHelper Slug { get; } = new(); | ||
|
||
static Generators() | ||
{ | ||
FolderName | ||
.RuleFor(p => p.Folder, f => f.Lorem.Slug(1)); | ||
|
||
Section | ||
.RuleFor(p => p.Paragraphs, f => f.Lorem.Paragraphs(f.Random.Number(1, 10))) | ||
.RuleFor(p => p.Level, f => f.Random.Number(2, 4)); | ||
|
||
File | ||
.Ignore(p => p.Links) | ||
.Ignore(p => p.Directory) | ||
.RuleFor(p => p.FileName, f => f.System.FileName("md")) | ||
.RuleFor(p => p.IncludeInUpdate, f => f.Random.Float() <= Determinism.Random.FileProbability) | ||
.RuleFor(p => p.Sections, f => Section.Generate(Determinism.Random.Contents.Number(1, 12)).ToArray()); | ||
} | ||
|
||
public static IEnumerable<string> CreateSubPaths(string parent, int maxDepth, int currentDepth) | ||
{ | ||
yield return parent; | ||
if (currentDepth == maxDepth) yield break; | ||
var subFolders = FolderName.Generate(Determinism.Random.FileSystem.Number(0, 4)); | ||
foreach (var subFolder in subFolders) | ||
{ | ||
var path = $"{parent}/{subFolder.Folder}"; | ||
yield return path; | ||
var subPaths = CreateSubPaths(path, maxDepth, currentDepth + 1); | ||
foreach (var p in subPaths) | ||
yield return p; | ||
} | ||
} | ||
|
||
public static string[] FolderNames { get; set; } = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Slugify; | ||
using Soenneker.Utils.AutoBogus; | ||
|
||
namespace Documentation.Generator.Domain; | ||
|
||
public record MarkdownFile | ||
{ | ||
public required string FileName { get; init; } | ||
public required Section[] Sections { get; init; } | ||
public required string Title { get; init; } | ||
|
||
public string RelativePath => $"{Directory}/{FileName}"; | ||
|
||
public required bool IncludeInUpdate { get; init; } = true; | ||
|
||
public string Directory { get; set; } = string.Empty; | ||
public List<string> Links { get; set; } = []; | ||
|
||
public void RewriteLinksIntoSections() | ||
{ | ||
var linksLength = Links.Count; | ||
var sectionsLength = Sections.Length; | ||
for (var i = 0; i < linksLength; i++) | ||
{ | ||
var link = Links[i]; | ||
var section = Sections[Determinism.Random.Contents.Number(0, sectionsLength - 1)]; | ||
var words = section.Paragraphs.Split(" "); | ||
var w = Determinism.Random.Contents.Number(0, words.Length - 1); | ||
var word = words[w]; | ||
words[w] = $"[{word}](/{link})"; | ||
section.Paragraphs = string.Join(" ", words); | ||
} | ||
} | ||
|
||
|
||
public string GetRandomLink() | ||
{ | ||
var sectionLink = Determinism.Random.Contents.Bool(0.8f); | ||
if (!sectionLink) return RelativePath; | ||
var section = Sections[Determinism.Random.Contents.Number(0, Sections.Length - 1)]; | ||
return $"{RelativePath}#{Generators.Slug.GenerateSlug(section.Header)}"; | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
namespace Documentation.Generator.Domain; | ||
|
||
public static class Paths | ||
{ | ||
private static DirectoryInfo RootDirectoryInfo() | ||
{ | ||
var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); | ||
while (directory != null && | ||
(directory.GetFiles("*.sln").Length == 0 || directory.GetDirectories(".git").Length == 0)) | ||
directory = directory.Parent; | ||
return directory ?? new DirectoryInfo(Directory.GetCurrentDirectory()); | ||
} | ||
|
||
public static readonly DirectoryInfo Root = RootDirectoryInfo(); | ||
} | ||
|
||
public record FolderName | ||
{ | ||
public required string Folder { get; init; } | ||
} | ||
|
||
public record Folder | ||
{ | ||
public required string Path { get; init; } | ||
public required MarkdownFile[] Files { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
namespace Documentation.Generator.Domain; | ||
|
||
public record Section | ||
{ | ||
public required string Header { get; init; } | ||
|
||
public required int Level { get; init; } | ||
|
||
public required string Paragraphs { get; set; } | ||
|
||
} |
Oops, something went wrong.