Skip to content

Commit

Permalink
refactor markdown output
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVollmers committed May 6, 2024
1 parent 5609774 commit bb083be
Showing 1 changed file with 69 additions and 77 deletions.
146 changes: 69 additions & 77 deletions src/Doki.Output.Markdown/MarkdownOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,61 @@ public async Task WriteAsync(DocumentationRoot root, CancellationToken cancellat
{
ArgumentNullException.ThrowIfNull(root);

var currentPath = contentList.GetPath();
var (file, markdown) = Prepare(root, root.Name);

var targetFile = new FileInfo(Path.Combine(options.OutputDirectory.FullName, currentPath, "README.md"));

if (!targetFile.Directory!.Exists) targetFile.Directory.Create();
var items = new List<Element>();
foreach (var assemblyDocumentation in root.Assemblies)
{
var container = new IndentContainer(1, false);
container.Add(markdown.BuildLinkTo(assemblyDocumentation));

var heading = new Heading(contentList.Name, 1);
if (contentList.Content == DocumentationContentType.Namespace) heading.Append(" Namespace");
if (assemblyDocumentation.Description != null)
{
container.Add(Text.Empty);
container.Add(new Text(assemblyDocumentation.Description));
}

var markdown = new MarkdownBuilder(currentPath).Add(heading);
items.Add(container);
}

if (contentList.Description != null)
markdown.Add(new List
{
markdown.Add(new Text(contentList.Description));
}
Items = items
});

await WriteMarkdownAsync(file, markdown, cancellationToken);
}

// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
switch (contentList.Content)
public async Task WriteAsync(AssemblyDocumentation assemblyDocumentation,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(assemblyDocumentation);

var (file, markdown) = Prepare(assemblyDocumentation, assemblyDocumentation.Name);

markdown.Add(new Heading("Namespaces", 2));
markdown.Add(new List
{
case DocumentationContentType.Assemblies:
await BuildAssembliesListAsync(markdown, contentList, cancellationToken);
break;
case DocumentationContentType.Assembly:
foreach (var item in contentList.Items)
{
if (item is not ContentList namespaceDocumentation) continue;
Items = assemblyDocumentation.Namespaces.Select(x => markdown.BuildLinkTo(x)).ToList()
});

await WriteAsync(namespaceDocumentation, cancellationToken);
}
await WriteMarkdownAsync(file, markdown, cancellationToken);
}

markdown.Add(new Heading("Namespaces", 2));
markdown.Add(new List
{
Items = contentList.Items.Select(x => markdown.BuildLinkTo(x)).ToList()
});
break;
case DocumentationContentType.Namespace:
markdown.Add(new Heading("Types", 2));
goto default;
default:
markdown.Add(new List
{
Items = contentList.Items.Select(x => BuildMarkdownList(markdown, x, 0)).ToList()
});
break;
}
public async Task WriteAsync(NamespaceDocumentation namespaceDocumentation,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(namespaceDocumentation);

var (file, markdown) = Prepare(namespaceDocumentation, namespaceDocumentation.Name);

await WriteMarkdownAsync(targetFile, markdown, cancellationToken);
markdown.Add(new Heading("Types", 2));
markdown.Add(new List
{
Items = namespaceDocumentation.Types.Select(x => markdown.BuildLinkTo(x)).ToList()
});

await WriteMarkdownAsync(file, markdown, cancellationToken);
}

public async Task WriteAsync(TypeDocumentation typeDocumentation, CancellationToken cancellationToken = default)
Expand All @@ -81,7 +88,8 @@ public async Task WriteAsync(TypeDocumentation typeDocumentation, CancellationTo
.Add(new Heading(typeDocumentation.Name, 1).Append($" {Enum.GetName(typeDocumentation.Content)}"))
.Add(new Heading(nameof(TypeDocumentation.Definition), 2));

var namespaceDocumentation = typeDocumentation.TryGetParent<ContentList>(DocumentationContentType.Namespace);
var namespaceDocumentation =
typeDocumentation.TryGetParent<NamespaceDocumentation>(DocumentationContentType.Namespace);
if (namespaceDocumentation != null)
{
markdown.Add(new Text("Namespace: ").Append(markdown.BuildLinkTo(namespaceDocumentation)));
Expand Down Expand Up @@ -251,34 +259,6 @@ public async Task WriteAsync(TypeDocumentation typeDocumentation, CancellationTo
await WriteMarkdownAsync(typeDocumentationFile, markdown, cancellationToken);
}

private async Task BuildAssembliesListAsync(MarkdownBuilder markdown, ContentList contentList,
CancellationToken cancellationToken)
{
var items = new List<Element>();
foreach (var item in contentList.Items)
{
if (item is not AssemblyDocumentation assemblyDocumentation) continue;

await WriteAsync(assemblyDocumentation, cancellationToken);

var container = new IndentContainer(1, false);
container.Add(markdown.BuildLinkTo(assemblyDocumentation));

if (assemblyDocumentation.Description != null)
{
container.Add(Text.Empty);
container.Add(new Text(assemblyDocumentation.Description));
}

items.Add(container);
}

markdown.Add(new List
{
Items = items
});
}

private static async Task WriteMarkdownAsync(FileSystemInfo fileInfo, MarkdownBuilder markdown,
CancellationToken cancellationToken)
{
Expand All @@ -301,16 +281,6 @@ private static async Task WriteMarkdownAsync(FileSystemInfo fileInfo, MarkdownBu
await File.WriteAllTextAsync(fileInfo.FullName, markdown.ToString(), cancellationToken);
}

private static Element BuildMarkdownList(MarkdownBuilder markdown, DocumentationObject element, int indent)
{
var link = markdown.BuildLinkTo(element);
if (element is not ContentList contentList || contentList.Items.Length == 0) return link;
return new SubList(link, indent)
{
Items = contentList.Items.Select(x => BuildMarkdownList(markdown, x, indent + 1)).ToList()
};
}

private static IEnumerable<Element> BuildInheritanceChain(MarkdownBuilder markdown,
TypeDocumentationReference typeDocumentationReference)
{
Expand All @@ -321,4 +291,26 @@ private static IEnumerable<Element> BuildInheritanceChain(MarkdownBuilder markdo
typeDocumentationReference = typeDocumentationReference.BaseType;
}
}

private (FileInfo, MarkdownBuilder) Prepare(DocumentationObject documentationObject, string name,
string? description = null)
{
var currentPath = documentationObject.GetPath();

var file = new FileInfo(Path.Combine(options.OutputDirectory.FullName, currentPath, "README.md"));

if (!file.Directory!.Exists) file.Directory.Create();

var heading = new Heading(name, 1);
if (documentationObject.Content == DocumentationContentType.Namespace) heading.Append(" Namespace");

var markdown = new MarkdownBuilder(documentationObject.GetPath()).Add(heading);

if (description != null)
{
markdown.Add(new Text(description));
}

return (file, markdown);
}
}

0 comments on commit bb083be

Please sign in to comment.