Skip to content

Commit

Permalink
added ExcludeGenerator to exclude DocItem from the documentation gene…
Browse files Browse the repository at this point in the history
…ration based on regex (closes #160)
  • Loading branch information
Doraku committed Oct 18, 2024
1 parent 18754aa commit 8e594aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dotnet tool
- [HandleLineBreak](#MarkdownConfiguration_HandleLineBreak)
- [TableOfContentsModes](#MarkdownConfiguration_TableOfContentsModes)
- [Url format](#MarkdownConfiguration_UrlFormat)
- [Exclude](#MarkdownConfiguration_Exclude)
- [Samples](#Samples)
- [Dependencies](#Dependencies)

Expand Down Expand Up @@ -329,8 +330,9 @@ The list of plugin files to load to create the documentation. See [Plugins](#Ove

`Name` or `Type Assembly` of the `IDocItemGenerator` implementations to use to generate the `DocItem` of the documentation.
The default implementations provided are:
- `Exclude` or `DefaultDocumentation.Markdown.DocItemGenerators.ExcludeGenerator DefaultDocumentation.Markdown` remove `DocItem` from the documentation generation based on [MarkdownConfiguration.Exclude](#MarkdownConfiguration_Exclude).
- `Overloads` or `DefaultDocumentation.Markdown.DocItemGenerators.OverloadsGenerator DefaultDocumentation.Markdown` adds pages to group constructor and method overloads the same way microsoft documentation do it.
The default value is `Overloads`.
The default value is `Exclude|Overloads`.

<a name='Configuration_UrlFactories'></a>
## UrlFactories
Expand Down Expand Up @@ -563,6 +565,15 @@ Three arguments will be passed to the format:

The default value is `[{0}]({1} '{2}')`.

<a name='MarkdownConfiguration_Exclude'></a>
## Exclude
- configuration file: `"Markdown.Exclude": [ "", ... ]`

Contains a collection of regex used by the `DefaultDocumentation.Markdown.DocItemGenerators.ExcludeGenerator DefaultDocumentation.Markdown` [DocItemGenerator](#Configuration_DocItemGenerators),
any `DocItem` whose id will match one of them will be excluded from the documentation generation.

The default value is `null`.

<a name='Samples'></a>
# Samples
- [DefaultDocumentation api](https://github.com/Doraku/DefaultDocumentation/blob/master/documentation/api/index.md)
Expand Down
1 change: 1 addition & 0 deletions documentation/NEXT_RELEASENOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- it is now possible to change the format of url link procuded in the markdown via Markdown.UrlFormat configuration (closes #107)
- added IDocItemGenerator to customize pages generated
- added OverloadsGenerator to put constructor and method overloads in the same page (closes #139)
- added ExcludeGenerator to exclude DocItem from the documentation generation based on regex (closes #160)

## Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion source/DefaultDocumentation.Common/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private Generator(Target loggerTarget, IRawSettings settings)
AddSetting(settings => settings.ExternLinksFilePaths, value => !(value ?? []).Any(), value => value.ToArray());
// context settings
AddSetting(settings => settings.Plugins, value => !(value ?? []).Any(), value => value.ToArray());
AddSetting(settings => settings.DocItemGenerators, value => !(value ?? []).Any(), ["Overloads"]);
AddSetting(settings => settings.DocItemGenerators, value => !(value ?? []).Any(), ["Exclude", "Overloads"]);
AddSetting(settings => settings.FileNameFactory, string.IsNullOrEmpty, "FullName");
AddSetting(settings => settings.UrlFactories, value => !(value ?? []).Any(), value => value.ToArray(), ["DocItem", "DotnetApi"]);
AddSetting(settings => settings.Sections, value => !(value ?? []).Any(), value => value.ToArray(), ["Header", "Default"]);
Expand Down
4 changes: 0 additions & 4 deletions source/DefaultDocumentation.Common/Internal/GeneralContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,10 @@ .. GetImplementations<IDocItemGenerator>(availableTypes, docItemGenerator => doc

#region IDocItemsContext

ISettings IDocItemsContext.Settings => Settings;

IDictionary<string, DocItem> IDocItemsContext.Items => _items;

ICollection<DocItem> IDocItemsContext.ItemsWithOwnPage => _itemsWithOwnPage;

T? IDocItemsContext.GetSetting<T>(string name) where T : default => GetSetting<T>(name);

T? IDocItemsContext.GetSetting<T>(Type? type, string name) where T : default => GetContext(type).GetSetting<T>(name);

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using DefaultDocumentation.Api;
using DefaultDocumentation.Models;

namespace DefaultDocumentation.Markdown.DocItemGenerators;

/// <summary>
/// Implementation of the <see cref="IDocItemGenerator"/> to remove <see cref="DocItem"/> from the documentation generation based on <see href="https://github.com/Doraku/DefaultDocumentation#MarkdownConfiguration_Exclude">Markdown.Exclude</see>.
/// </summary>
public sealed class ExcludeGenerator : IDocItemGenerator
{
/// <summary>
/// The name of this implementation used at the configuration level.
/// </summary>
public const string ConfigName = "Exclude";

/// <inheritdoc/>
public string Name => ConfigName;

/// <inheritdoc/>
public void Generate(IDocItemsContext context)
{
context.ThrowIfNull();

void Remove(DocItem item)
{
context.Items.Remove(item.Id);
context.ItemsWithOwnPage.Remove(item);

foreach (DocItem child in context.Items.Values.Where(child => child.Parent == item).ToArray())
{
Remove(child);
}
}

foreach (DocItem item in (context.GetSetting<string[]>("Markdown.Exclude") ?? [])
.SelectMany(regex => context.Items.Values.Where(item => Regex.IsMatch(item.Id, regex)))
.ToArray())
{
Remove(item);
}
}
}

0 comments on commit 8e594aa

Please sign in to comment.