Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true

# just a suggestion do to our JSON tests that use anonymous types to
# represent json quite a bit (makes copy paste easier).
csharp_new_line_before_members_in_anonymous_types = true
Expand All @@ -184,6 +185,7 @@ csharp_space_between_method_call_parameter_list_parentheses = false
#Wrap
csharp_preserve_single_line_statements = false
csharp_preserve_single_line_blocks = true
resharper_wrap_object_and_collection_initializer_style = chop_always

# Resharper
resharper_csharp_braces_for_lock=required_for_multiline
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Markdown.Refactor/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ string targetPath
{
var relativeSource = Path.GetRelativePath(currentDir, sourcePath);
var relativeSourceWithDotSlash = Path.Combine(".", relativeSource);
var relativeToDocsFolder = Path.GetRelativePath(documentationSet.SourcePath.FullName, sourcePath);
var relativeToDocsFolder = Path.GetRelativePath(documentationSet.SourceDirectory.FullName, sourcePath);
var absolutStyleSource = $"/{relativeToDocsFolder}";
var relativeToDocsFolderTarget = Path.GetRelativePath(documentationSet.SourcePath.FullName, targetPath);
var relativeToDocsFolderTarget = Path.GetRelativePath(documentationSet.SourceDirectory.FullName, targetPath);
var absoluteStyleTarget = $"/{relativeToDocsFolderTarget}";
return (
relativeSource,
Expand Down
16 changes: 8 additions & 8 deletions src/Elastic.Markdown/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public record BuildContext
public IFileSystem ReadFileSystem { get; }
public IFileSystem WriteFileSystem { get; }

public IDirectoryInfo SourcePath { get; }
public IDirectoryInfo OutputPath { get; }
public IDirectoryInfo DocumentationSourceDirectory { get; }
public IDirectoryInfo DocumentationOutputDirectory { get; }

public IFileInfo ConfigurationPath { get; }

Expand Down Expand Up @@ -56,17 +56,17 @@ public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem,
? ReadFileSystem.DirectoryInfo.New(source)
: ReadFileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName));

(SourcePath, ConfigurationPath) = FindDocsFolderFromRoot(rootFolder);
(DocumentationSourceDirectory, ConfigurationPath) = FindDocsFolderFromRoot(rootFolder);

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

if (ConfigurationPath.FullName != SourcePath.FullName)
SourcePath = ConfigurationPath.Directory!;
if (ConfigurationPath.FullName != DocumentationSourceDirectory.FullName)
DocumentationSourceDirectory = ConfigurationPath.Directory!;

Git = GitCheckoutInformation.Create(SourcePath, ReadFileSystem);
Configuration = new ConfigurationFile(ConfigurationPath, SourcePath, this);
Git = GitCheckoutInformation.Create(DocumentationSourceDirectory, ReadFileSystem);
Configuration = new ConfigurationFile(ConfigurationPath, DocumentationSourceDirectory, this);
}

public ConfigurationFile Configuration { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void EmitError(this InlineProcessor processor, int line, int colum
var d = new Diagnostic
{
Severity = Severity.Error,
File = processor.GetContext().Path.FullName,
File = processor.GetContext().MarkdownSourcePath.FullName,
Column = column,
Line = line,
Message = message,
Expand All @@ -38,7 +38,7 @@ public static void EmitWarning(this InlineProcessor processor, int line, int col
var d = new Diagnostic
{
Severity = Severity.Warning,
File = processor.GetContext().Path.FullName,
File = processor.GetContext().MarkdownSourcePath.FullName,
Column = column,
Line = line,
Message = message,
Expand All @@ -54,7 +54,7 @@ public static void EmitError(this ParserContext context, string message, Excepti
var d = new Diagnostic
{
Severity = Severity.Error,
File = context.Path.FullName,
File = context.MarkdownSourcePath.FullName,
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
};
context.Build.Collector.Channel.Write(d);
Expand All @@ -67,7 +67,7 @@ public static void EmitWarning(this ParserContext context, int line, int column,
var d = new Diagnostic
{
Severity = Severity.Warning,
File = context.Path.FullName,
File = context.MarkdownSourcePath.FullName,
Column = column,
Line = line,
Message = message,
Expand Down Expand Up @@ -146,7 +146,7 @@ public static void EmitError(this InlineProcessor processor, LinkInline inline,
var d = new Diagnostic
{
Severity = Severity.Error,
File = processor.GetContext().Path.FullName,
File = processor.GetContext().MarkdownSourcePath.FullName,
Column = column,
Line = line,
Message = message,
Expand All @@ -169,7 +169,7 @@ public static void EmitWarning(this InlineProcessor processor, LinkInline inline
var d = new Diagnostic
{
Severity = Severity.Warning,
File = processor.GetContext().Path.FullName,
File = processor.GetContext().MarkdownSourcePath.FullName,
Column = column,
Line = line,
Message = message,
Expand Down
16 changes: 8 additions & 8 deletions src/Elastic.Markdown/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ public DocumentationGenerator(
)
{
_conversionCollector = conversionCollector;
_readFileSystem = docSet.Context.ReadFileSystem;
_writeFileSystem = docSet.Context.WriteFileSystem;
_readFileSystem = docSet.Build.ReadFileSystem;
_writeFileSystem = docSet.Build.WriteFileSystem;
_logger = logger.CreateLogger(nameof(DocumentationGenerator));

DocumentationSet = docSet;
Context = docSet.Context;
Context = docSet.Build;
Resolver = docSet.LinkResolver;
HtmlWriter = new HtmlWriter(DocumentationSet, _writeFileSystem);

_logger.LogInformation("Created documentation set for: {DocumentationSetName}", DocumentationSet.Name);
_logger.LogInformation("Source directory: {SourcePath} Exists: {SourcePathExists}", docSet.SourcePath, docSet.SourcePath.Exists);
_logger.LogInformation("Output directory: {OutputPath} Exists: {OutputPathExists}", docSet.OutputPath, docSet.OutputPath.Exists);
_logger.LogInformation("Source directory: {SourcePath} Exists: {SourcePathExists}", docSet.SourceDirectory, docSet.SourceDirectory.Exists);
_logger.LogInformation("Output directory: {OutputPath} Exists: {OutputPathExists}", docSet.OutputDirectory, docSet.OutputDirectory.Exists);
}

public GenerationState? GetPreviousGenerationState()
Expand Down Expand Up @@ -181,7 +181,7 @@ private async Task ProcessFile(HashSet<string> offendingFiles, DocumentationFile

private IFileInfo OutputFile(string relativePath)
{
var outputFile = _writeFileSystem.FileInfo.New(Path.Combine(DocumentationSet.OutputPath.FullName, relativePath));
var outputFile = _writeFileSystem.FileInfo.New(Path.Combine(DocumentationSet.OutputDirectory.FullName, relativePath));
return outputFile;
}

Expand Down Expand Up @@ -227,7 +227,7 @@ private async Task GenerateLinkReference(Cancel ctx)
var state = LinkReference.Create(DocumentationSet);

var bytes = JsonSerializer.SerializeToUtf8Bytes(state, SourceGenerationContext.Default.LinkReference);
await DocumentationSet.OutputPath.FileSystem.File.WriteAllBytesAsync(file.FullName, bytes, ctx);
await DocumentationSet.OutputDirectory.FileSystem.File.WriteAllBytesAsync(file.FullName, bytes, ctx);
}

private async Task GenerateDocumentationState(Cancel ctx)
Expand All @@ -242,7 +242,7 @@ private async Task GenerateDocumentationState(Cancel ctx)
Git = Context.Git
};
var bytes = JsonSerializer.SerializeToUtf8Bytes(state, SourceGenerationContext.Default.GenerationState);
await DocumentationSet.OutputPath.FileSystem.File.WriteAllBytesAsync(stateFile.FullName, bytes, ctx);
await DocumentationSet.OutputDirectory.FileSystem.File.WriteAllBytesAsync(stateFile.FullName, bytes, ctx);
}

private async Task CopyFileFsAware(DocumentationFile file, IFileInfo outputFile, Cancel ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
using DotNet.Globbing;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.IO.State;
using YamlDotNet.Core;
using YamlDotNet.RepresentationModel;

namespace Elastic.Markdown.IO.Configuration;

public record ConfigurationFile : DocumentationFile
{
private readonly IFileInfo _sourceFile;
private readonly IDirectoryInfo _rootPath;
private readonly BuildContext _context;
private readonly int _depth;
Expand All @@ -36,7 +34,6 @@ public record ConfigurationFile : DocumentationFile
public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildContext context, int depth = 0, string parentPath = "")
: base(sourceFile, rootPath)
{
_sourceFile = sourceFile;
_rootPath = rootPath;
_context = context;
_depth = depth;
Expand All @@ -52,7 +49,7 @@ public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildCon
var redirectFile = new RedirectFile(redirectFileInfo, _context);
Redirects = redirectFile.Redirects;

var reader = new YamlStreamReader(_sourceFile, _context);
var reader = new YamlStreamReader(sourceFile, _context);
try
{
foreach (var entry in reader.Read())
Expand Down
1 change: 0 additions & 1 deletion src/Elastic.Markdown/IO/Configuration/RedirectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information

using System.IO.Abstractions;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.IO.State;
using YamlDotNet.RepresentationModel;

Expand Down
90 changes: 47 additions & 43 deletions src/Elastic.Markdown/IO/DocumentationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,68 @@
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.IO.Configuration;
using Elastic.Markdown.IO.Navigation;
using Elastic.Markdown.IO.State;
using Elastic.Markdown.Myst;
using Microsoft.Extensions.Logging;

namespace Elastic.Markdown.IO;

public class DocumentationSet
{
public BuildContext Context { get; }
public BuildContext Build { get; }
public string Name { get; }
public IFileInfo OutputStateFile { get; }
public IFileInfo LinkReferenceFile { get; }

public IDirectoryInfo SourcePath { get; }
public IDirectoryInfo OutputPath { get; }
public IDirectoryInfo SourceDirectory { get; }
public IDirectoryInfo OutputDirectory { get; }

public string RelativeSourcePath { get; }

public DateTimeOffset LastWrite { get; }

public ConfigurationFile Configuration { get; }

public MarkdownParser MarkdownParser { get; }
private MarkdownParser MarkdownParser { get; }

public ICrossLinkResolver LinkResolver { get; }

public DocumentationSet(BuildContext context, ILoggerFactory logger, ICrossLinkResolver? linkResolver = null)
public DocumentationSet(BuildContext build, ILoggerFactory logger, ICrossLinkResolver? linkResolver = null)
{
Context = context;
SourcePath = context.SourcePath;
OutputPath = context.OutputPath;
RelativeSourcePath = Path.GetRelativePath(Paths.Root.FullName, SourcePath.FullName);
Build = build;
SourceDirectory = build.DocumentationSourceDirectory;
OutputDirectory = build.DocumentationOutputDirectory;
RelativeSourcePath = Path.GetRelativePath(Paths.Root.FullName, SourceDirectory.FullName);
LinkResolver =
linkResolver ?? new CrossLinkResolver(new ConfigurationCrossLinkFetcher(context.Configuration, logger));
Configuration = context.Configuration;
linkResolver ?? new CrossLinkResolver(new ConfigurationCrossLinkFetcher(build.Configuration, logger));
Configuration = build.Configuration;

MarkdownParser = new MarkdownParser(SourcePath, context, GetMarkdownFile, context.Configuration, LinkResolver);
var resolver = new ParserResolvers
{
CrossLinkResolver = LinkResolver,
DocumentationFileLookup = DocumentationFileLookup
};
MarkdownParser = new MarkdownParser(build, resolver);

Name = SourcePath.FullName;
OutputStateFile = OutputPath.FileSystem.FileInfo.New(Path.Combine(OutputPath.FullName, ".doc.state"));
LinkReferenceFile = OutputPath.FileSystem.FileInfo.New(Path.Combine(OutputPath.FullName, "links.json"));
Name = SourceDirectory.FullName;
OutputStateFile = OutputDirectory.FileSystem.FileInfo.New(Path.Combine(OutputDirectory.FullName, ".doc.state"));
LinkReferenceFile = OutputDirectory.FileSystem.FileInfo.New(Path.Combine(OutputDirectory.FullName, "links.json"));

Files = [.. context.ReadFileSystem.Directory
.EnumerateFiles(SourcePath.FullName, "*.*", SearchOption.AllDirectories)
.Select(f => context.ReadFileSystem.FileInfo.New(f))
Files = [.. build.ReadFileSystem.Directory
.EnumerateFiles(SourceDirectory.FullName, "*.*", SearchOption.AllDirectories)
.Select(f => build.ReadFileSystem.FileInfo.New(f))
.Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden) && !f.Attributes.HasFlag(FileAttributes.System))
.Where(f => !f.Directory!.Attributes.HasFlag(FileAttributes.Hidden) && !f.Directory!.Attributes.HasFlag(FileAttributes.System))
// skip hidden folders
.Where(f => !Path.GetRelativePath(SourcePath.FullName, f.FullName).StartsWith('.'))
.Where(f => !Path.GetRelativePath(SourceDirectory.FullName, f.FullName).StartsWith('.'))
.Select<IFileInfo, DocumentationFile>(file => file.Extension switch
{
".jpg" => new ImageFile(file, SourcePath, "image/jpeg"),
".jpeg" => new ImageFile(file, SourcePath, "image/jpeg"),
".gif" => new ImageFile(file, SourcePath, "image/gif"),
".svg" => new ImageFile(file, SourcePath, "image/svg+xml"),
".png" => new ImageFile(file, SourcePath),
".md" => CreateMarkDownFile(file, context),
_ => new StaticFile(file, SourcePath)
".jpg" => new ImageFile(file, SourceDirectory, "image/jpeg"),
".jpeg" => new ImageFile(file, SourceDirectory, "image/jpeg"),
".gif" => new ImageFile(file, SourceDirectory, "image/gif"),
".svg" => new ImageFile(file, SourceDirectory, "image/svg+xml"),
".png" => new ImageFile(file, SourceDirectory),
".md" => CreateMarkDownFile(file, build),
_ => new StaticFile(file, SourceDirectory)
})];

LastWrite = Files.Max(f => f.SourceFile.LastWriteTimeUtc);
Expand All @@ -77,7 +81,7 @@ public DocumentationSet(BuildContext context, ILoggerFactory logger, ICrossLinkR
.ToDictionary(g => g.Key, g => g.ToArray());

var fileIndex = 0;
Tree = new DocumentationGroup(Context, Configuration.TableOfContents, FlatMappedFiles, folderFiles, ref fileIndex)
Tree = new DocumentationGroup(Build, Configuration.TableOfContents, FlatMappedFiles, folderFiles, ref fileIndex)
{
Parent = null
};
Expand All @@ -86,7 +90,7 @@ public DocumentationSet(BuildContext context, ILoggerFactory logger, ICrossLinkR

var excludedChildren = markdownFiles.Where(f => f.NavigationIndex == -1).ToArray();
foreach (var excludedChild in excludedChildren)
Context.EmitError(Context.ConfigurationPath, $"{excludedChild.RelativePath} is unreachable in the TOC because one of its parents matches exclusion glob");
Build.EmitError(Build.ConfigurationPath, $"{excludedChild.RelativePath} is unreachable in the TOC because one of its parents matches exclusion glob");

MarkdownFiles = markdownFiles.Where(f => f.NavigationIndex > -1).ToDictionary(i => i.NavigationIndex, i => i).ToFrozenDictionary();
ValidateRedirectsExists();
Expand Down Expand Up @@ -114,14 +118,14 @@ void ValidateExists(string from, string to, IReadOnlyDictionary<string, string?>
{
if (!FlatMappedFiles.TryGetValue(to, out var file))
{
Context.EmitError(Configuration.SourceFile, $"Redirect {from} points to {to} which does not exist");
Build.EmitError(Configuration.SourceFile, $"Redirect {from} points to {to} which does not exist");
return;

}

if (file is not MarkdownFile markdownFile)
{
Context.EmitError(Configuration.SourceFile, $"Redirect {from} points to {to} which is not a markdown file");
Build.EmitError(Configuration.SourceFile, $"Redirect {from} points to {to} which is not a markdown file");
return;
}

Expand All @@ -138,9 +142,9 @@ void ValidateExists(string from, string to, IReadOnlyDictionary<string, string?>

public FrozenDictionary<int, MarkdownFile> MarkdownFiles { get; }

public MarkdownFile? GetMarkdownFile(IFileInfo sourceFile)
public MarkdownFile? DocumentationFileLookup(IFileInfo sourceFile)
{
var relativePath = Path.GetRelativePath(SourcePath.FullName, sourceFile.FullName);
var relativePath = Path.GetRelativePath(SourceDirectory.FullName, sourceFile.FullName);
if (FlatMappedFiles.TryGetValue(relativePath, out var file) && file is MarkdownFile markdownFile)
return markdownFile;
return null;
Expand Down Expand Up @@ -184,26 +188,26 @@ public async Task ResolveDirectoryTree(Cancel ctx) =>

private DocumentationFile CreateMarkDownFile(IFileInfo file, BuildContext context)
{
var relativePath = Path.GetRelativePath(SourcePath.FullName, file.FullName);
var relativePath = Path.GetRelativePath(SourceDirectory.FullName, file.FullName);
if (Configuration.Exclude.Any(g => g.IsMatch(relativePath)))
return new ExcludedFile(file, SourcePath);
return new ExcludedFile(file, SourceDirectory);

// we ignore files in folders that start with an underscore
if (relativePath.Contains("_snippets"))
return new SnippetFile(file, SourcePath);
return new SnippetFile(file, SourceDirectory);

if (Configuration.Files.Contains(relativePath))
return new MarkdownFile(file, SourcePath, MarkdownParser, context, this);
return new MarkdownFile(file, SourceDirectory, MarkdownParser, context, this);

if (Configuration.Globs.Any(g => g.IsMatch(relativePath)))
return new MarkdownFile(file, SourcePath, MarkdownParser, context, this);
return new MarkdownFile(file, SourceDirectory, MarkdownParser, context, this);

// we ignore files in folders that start with an underscore
if (relativePath.IndexOf("/_", StringComparison.Ordinal) > 0 || relativePath.StartsWith('_'))
return new ExcludedFile(file, SourcePath);
return new ExcludedFile(file, SourceDirectory);

context.EmitError(Configuration.SourceFile, $"Not linked in toc: {relativePath}");
return new ExcludedFile(file, SourcePath);
return new ExcludedFile(file, SourceDirectory);
}

public DocumentationGroup Tree { get; }
Expand All @@ -214,8 +218,8 @@ private DocumentationFile CreateMarkDownFile(IFileInfo file, BuildContext contex

public void ClearOutputDirectory()
{
if (OutputPath.Exists)
OutputPath.Delete(true);
OutputPath.Create();
if (OutputDirectory.Exists)
OutputDirectory.Delete(true);
OutputDirectory.Create();
}
}
Loading