• Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
    C#
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A standalone API to read .NET XML documentation files and optionally augment it with reflection information.

Icon NuDoq: A lightweight .NET XML Documentation API

A standalone API to read and write .NET XML documentation files and optionally augment it with reflection information.

Version Downloads License

NuDoq provides a simple and intuitive API that reads .NET XML documentation files into an in-memory model that can be easily used to generate alternative representations or arbitrary processing. If the read operation is performed using a .NET assembly rather than an XML file, NuDoq will automatically add the reflection information to the in-memory model for the documentation elements, making it very easy to post-process them by grouping by type, namespace, etc.

NuDoq leverages two well-known patterns: the Visitor pattern and the Composite pattern. Essentially, every member in the documentation file is represented as a separate "visitable" type. By simply writing a NuDoq Visitor-derived class, you can process only the elements you're interested in.

NuDoq can read documentation files from any .NET assembly, which are most commonly located alongside the binary.

How to Install

NuDoq is a single assembly with no external dependencies whatsoever and is distributed as a NuGet package. It can be installed issuing the following command in the Package Manager Console:

PM> Install-Package NuDoq

Usage

The main API is DocReader, which you can use to read an XML documentation file, or an assembly, in which case the XML file alongside the assembly location will be read and augmented with reflection information about types and members:

var members = DocReader.Read(typeof(MyType).Assembly);

You can then directly enumerate the members and their elements, but this is tedious and would involve a lot of if/switch statements to account for all the various types of elements and their nesting. This is why the main consumption is though a visitor. If you are not familiar with the pattern here is a good overview.

Essentially, you create a visitor and then override/implement methods for the relevant element kinds you're interested in.

Here is a sample visitor that would render markdown for the given nodes:

public class MarkdownVisitor : Visitor
{
    TextWriter output;

    public MarkdownVisitor(TextWriter output) 
        => this.output = output;

    public override void VisitMember(Member member)
    {
        output.WriteLine();
        output.WriteLine(new string('-', 50));
        output.WriteLine("# " + member.Id);
        base.VisitMember(member);
    }

    public override void VisitSummary(Summary summary)
    {
        output.WriteLine();
        output.WriteLine("## Summary");
        base.VisitSummary(summary);
    }

    public override void VisitRemarks(Remarks remarks)
    {
        output.WriteLine();
        output.WriteLine("## Remarks");
        base.VisitRemarks(remarks);
    }

    public override void VisitExample(Example example)
    {
        output.WriteLine();
        output.WriteLine("### Example");
        base.VisitExample(example);
    }

    public override void VisitC(C code)
    {
        // Wrap inline code in ` according to Markdown syntax.
        output.Write(" `");
        output.Write(code.Content);
        output.Write("` ");

        base.VisitC(code);
    }

    public override void VisitCode(Code code)
    {
        output.WriteLine();
        output.WriteLine();
        
        // Indent code with 4 spaces according to Markdown syntax.
        foreach (var line in code.Content.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
        {
            output.Write("    ");
            output.WriteLine(line);
        }

        output.WriteLine();
        base.VisitCode(code);
    }

    public override void VisitText(Text text)
    {
        output.Write(text.Content);
        base.VisitText(text);
    }

    public override void VisitPara(Para para)
    {
        output.WriteLine();
        output.WriteLine();
        base.VisitPara(para);
        output.WriteLine();
        output.WriteLine();
    }

    public override void VisitSee(See see)
    {
        var cref = NormalizeLink(see.Cref);
        output.Write(" [{0}]({1}) ", cref.Substring(2), cref);
    }

    public override void VisitSeeAlso(SeeAlso seeAlso)
    {
        var cref = NormalizeLink(seeAlso.Cref);
        output.WriteLine("[{0}]({1})", cref.Substring(2), cref);
    }

    string NormalizeLink(string cref)
        => cref.Replace(":", "-").Replace("(", "-").Replace(")", "");
}

And you would use it as follows:

var members = DocReader.Read(typeof(MyType).Assembly);
var visitor = new MarkdownVisitor(Console.Out);

// This would traverse all nodes, recursive, and Visit* methods appropriately
members.Accept(visitor);

There is also an XmlVisitor in the library that can generate the XML doc file from the in-memory model too, if you want to create (or modify) the documented members.

There is also a built-in DelegateVisitor which is useful to traverse the entire tree to process only specific nodes. For example, if you wanted to validate all the links in see/seealso elements, you'd use something like:

var members = DocReader.Read(typeof(MyType).Assembly);

var visitor = new DelegateVisitor(new VisitorDelegates
{
    VisitSee = see => ValidateUrl(see.Href),
    VisitSeeAlso = seealso => ValidateUrl(seealso.Href),
});

members.Accept(visitor);

Model

Given the main API to traverse and act on the documentation elements is through the visitor pattern, the most important part of the API is knowing the types of nodes/elements in the visitable model.

There are two logically separated hierarchies of visitable elements: the members (like the whole set read by the DocReader, a type, method, property, etc.) and the documentation elements (like summary, remarks, code, etc.).

The following is the members hierarchy:

Members hierarchy

And this is the supported documentation elements hierarchy:

Members hierarchy

Note that at the visitor level, both hierarchies are treated uniformly, since they all ultimately inherit from Element. In this fashion, you can have one or multiple visitors processing different parts of the graph, such as one that processes members and generates individual folders for each, and one for documentation elements that generate the content.

Sponsors

Clarius Org Kirill Osenkov MFB Technologies, Inc. Stephen Shaw Torutek DRIVE.NET, Inc. Daniel GnΓ€gi Ashley Medway Keith Pickford Thomas Bolon Kori Francis Toni Wenzel Giorgi Dalakishvili Mike James Dan Siegel Reuben Swartz Jacob Foshee Eric Johnson Norman Mackay Certify The Web Rich Lee Ix Technologies B.V. David JENNI Jonathan Oleg Kyrylchuk Mariusz Kogut Charley Wu Jakob TikjΓΈb Andersen Seann Alexander Tino Hager Mark Seemann Angelo Belchior Blauhaus Technology (Pty) Ltd Ken Bonny Simon Cropp agileworks-eu

Sponsor this project Β 

Learn more about GitHub Sponsors

More Repositories

1

moq

The most popular and friendly mocking framework for .NET
C#
5,709
star
2

GitInfo

Git and SemVer Info from MSBuild, C# and VB
Pascal
513
star
3

ThisAssembly

Exposes project and assembly level information as constants in the ThisAssembly class using source generators powered by Roslyn.
C#
418
star
4

SmallSharp

Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.
C#
279
star
5

nugetizer

A simple to understand packing model for authoring NuGet packages
C#
253
star
6

avatar

A modern compile-time generated interception/proxy library
C#
139
star
7

dotnet-file

Download, update and sync loose files from URLs
C#
52
star
8

dotnet-vs

A global tool for managing Visual Studio installations
C#
49
star
9

DependencyInjection.Attributed

Provides compile-time discovery and code generation of service registrations from attributed types
C#
36
star
10

CloudActors

An opinionated, simplified and uniform Cloud Native actors' library that integrates with Microsoft Orleans.
C#
33
star
11

RxFree

An ultra-lightweight Rx source-only nuget to avoid depending on the full System.Reactive for IObservable<T> producers
C#
32
star
12

SponsorLink

SponsorLink: an attempt at OSS sustainability
C#
31
star
13

xunit.assemblyfixture

Provides per-assembly fixture state support for xunit
C#
24
star
14

WebSocketPipe

System.IO.Pipelines API adapter for System.Net.WebSockets
C#
24
star
15

Merq

Internal application architecture via command and event messages
C#
23
star
16

TableStorage

Repository pattern with POCO object support for storing to Azure / Cosmos DB Table Storage
C#
21
star
17

Injector

Allows injecting .NET code into another Windows process
C++
12
star
18

WebSocketeer

High-performance intuitive API for Azure Web PubSub protobuf subprotocol
C#
11
star
19

xunit.vsix

Xunit for creating VSIX integration tests
C#
10
star
20

dotnet-tor

A .NET cross-platform CLI app that uses TorSharp to run a local proxy
C#
10
star
21

oss

Basic repo configuration for my OSS projects
SCSS
10
star
22

azdo

Home for azdo.io and accompanying linkinator source
C#
9
star
23

Dynamically

Instantiate record types from dynamic data with compatible structural shapes, in-memory with no reflection or serialization, via compile-time source generators.
C#
9
star
24

Web

XLinq to Web
HTML
8
star
25

chromium

Run a portable Chromium using dotnet 6+ and nuget.
C#
8
star
26

WebSocketChannel

High-performance System.Threading.Channels API adapter for System.Net.WebSockets
C#
8
star
27

dotnet-evergreen

A dotnet tool runner that automatically updates the tool package before and while running it
C#
6
star
28

CredentialManager

Packages the Git Credential Manager cross-platform implementation for Windows, macOS and Linux for use as a generic credential manager.
C#
6
star
29

dotnet-gcm

A dotnet global tool to interface with the Git Credentials Manager Core
C#
5
star
30

dotnet-eventgrid

Azure Function app and accompanying dotnet global tool to monitor Azure EventGrid streams in real-time.
C#
5
star
31

System.Diagnostics.Tracer

An improved API on top of System.Diagnostics
C#
5
star
32

json

JsonPeek and JsonPoke tasks implementations
C#
5
star
33

dotnet-stop

Gracefully stops processes by sending them SIGINT (Ctrl+C) in a cross platform way.
C#
4
star
34

nosayudamos.org

C#
3
star
35

catbag

A repository of loose helpers, base clases and assorted code
C#
3
star
36

scraper

A web scraping app that runs on Azure Container Apps
C#
3
star
37

actions-bot

A GitHub Action that sets bot secrets as git defaults if present
3
star
38

tsh

C#
2
star
39

cloudy

Spike on cloud-first architecture for apps, heavily event-driven
C#
2
star
40

PackageReferenceCleaner

Clean your PackageReferences with PrivateAssets=all into beautiful one-liners, automatically
C#
2
star
41

shields

Custom endpoints for custom badges using https://shields.io/endpoint
C#
2
star
42

Cognitive

Unified Cognitive API for Azure, Amazon and Google
C#
2
star
43

html

Loads Html documents as XLinq XDocuments
HTML
2
star
44

epub

A lightweight library that implements EPUB standard
C#
2
star
45

sponsors

Automatically updated list of devlooped sponsors
1
star
46

yaml

YamlPeek MSBuild Task
C#
1
star
47

actions-includes

Processes include HTML comments in files and inserts the included files
PowerShell
1
star
48

jq

A nuget distribution of the official JQ implementation, for easy consumption from .NET
C#
1
star
49

Azure.Functions.OpenApi

A zero-code OpenAPI (Swagger) basic generator for Azure Functions
C#
1
star
50

Mvp.Xml

C#
1
star
51

StreamR

An opinionated streaming-based library for running assistants with support for tool/function invocation and assistant behavior composition.
C#
1
star