DV8.Html 2.19.0

dotnet add package DV8.Html --version 2.19.0
NuGet\Install-Package DV8.Html -Version 2.19.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DV8.Html" Version="2.19.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DV8.Html --version 2.19.0
#r "nuget: DV8.Html, 2.19.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DV8.Html as a Cake Addin
#addin nuget:?package=DV8.Html&version=2.19.0

// Install DV8.Html as a Cake Tool
#tool nuget:?package=DV8.Html&version=2.19.0

DV8.Html

C# package with a HTML DSL and support for generating HTML elements and serializing object graphs to HTML

This project is a dead simple and dependency free package to work with HTML elements from C# code.

In addition, there is support for serializing objects and graphs of objects to HTML.

Lots of elements and attributes are implemented, and you can generate missing elements/attributes at run time by specifying element/attribute names.

Writing out non-standard / non-safe HTML code is also supported.

Various helper methods are available to make it easy to work with attributes and elements.

See the test classes for more info.

Requirements/Installation/Usage

Requirements: .Net Core 6.0 or later.

Dependencies: None.

Nuget link: https://www.nuget.org/packages/DV8.Html/

Usage:

dotnet add package DV8.Html

Using the DSL-like syntax for generating HTML

   using static DV8.Html.Prefixes.Underscore;
    ...
   var fruits = new[] { "Apple", "Banana", "Cherry" };
   var html =
        _<Html>(
            _<Head>(
                _<Title>("Hello, World!")
            ),
            _<Body>(
                _<H1>("Hello, World!"),
                _<P>(
                    _("This is a paragraph with <>. "), // Becomes plain text, not an element. Text is escaped. 
                    _<Ul>(
                        fruits.Select(_<Li>)
                    )
                ),
                _UNSAFE("This will not be <b>escaped</b>") // Allows any HTML, don't use this with untrusted content. 
            )
        );
    var act = html.ToHtml();
    var exp = @"
    <!DOCTYPE html><html>
    <head><title>Hello, World!</title></head>
    <body><h1>Hello, World!</h1><p>This is a paragraph with &lt;&gt;. <ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul></p>
    This will not be <b>escaped</b>
    </body></html>";

    // "Canonical" strips linebreaks, whitespace between elements, and uses ' instead of " as attribute delimiter.
    Assert.AreEqual(exp.Canonical(), act).Canonical();

Using plain C#

var fruits = new[] { "Apple", "Banana", "Cherry" }
    .Select( f => new A( $"https://fruits.org/{f}));
var ul = new Ul(fruits)
    .WithClass("the-fruits");
ul.Attributes["my-attribute"] = "my-value";
ul.Properties["my-property"] = myFruitCollectionObject;
var p = new P("This is a paragraph with <>. ", ul);

var ulHtml = ul.ToHtml()

Generating XML / XHTML

Use ToXml instead of ToHtml if you want the output to be correct XML. This uses the .Net XmlWriter class and is probably safer and faster, however it will always close elements, so an input becomes <input ... /> instead of <input ...>. In addition, it will use " instead of ' as attribute delimiter.

Serialization

Example code for serializing objects to HTML (recursive to max 3 levels into properties)

var ser = HtmlSerializerRegistry.AddDefaults(new HtmlSerializerRegistry()); 
var elements = HtmlSerializer.Serialize(myListOrCustomObjectOrWhatever, 3);

This serializer can also be added as a HtmlOutputFormatter in Asp.Net, easily making all your JSON-APIs available as straight, human-readable HTML.

Semantic HTML / microformats / S

Documentation TBA 😃

Contributing

In the very unlikely event that anybody actually is interested in this project: Let me know (starring it on github is enough) and I'll improve documentation and samples 😃 Issues and pull requests are also welcome.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DV8.Html:

Package Downloads
Nodes.API

Models and services for Nodes API

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.19.0 2,883 10/24/2023
2.18.0 136 10/24/2023
2.16.0 134 10/20/2023
2.15.0 128 10/20/2023
2.14.0 111 10/19/2023
2.13.0 436 10/8/2023
2.12.0 140 10/4/2023
2.11.0 137 10/2/2023
2.10.0 343 10/2/2023
2.8.0 112 10/2/2023
2.7.0 133 10/2/2023
2.6.0 123 10/2/2023
2.5.0 117 10/2/2023
2.4.0 113 10/1/2023
2.3.0 119 10/1/2023
2.2.0 141 10/1/2023
2.1.0 119 10/1/2023
2.0.0 121 10/1/2023
1.10.0 134 9/30/2023
1.9.0 120 9/30/2023
1.8.0 116 9/30/2023
1.7.0 125 9/30/2023
1.6.0 117 9/30/2023
1.4.0 124 9/30/2023
1.3.0 17,690 2/8/2022
1.2.0 509 2/7/2022
1.1.8 351 9/22/2021
1.1.6 323 9/8/2021
1.1.5 46,652 4/7/2021
1.1.4 298 4/7/2021
1.1.3 307 3/10/2021
1.1.2 1,543 3/7/2021
1.1.1 271 3/5/2021
1.1.0 320 2/9/2021
1.0.9 344 2/9/2021
1.0.8 1,857 8/11/2020
1.0.7 475 4/13/2020
1.0.6 2,745 11/1/2019
1.0.5 593 10/17/2019
1.0.4 554 10/15/2019
1.0.3 514 10/15/2019
1.0.2 589 9/19/2019
1.0.1 557 9/18/2019
1.0.0 546 9/18/2019