HtmlWriter Class
- Namespace
- Kampute.DocToolkit.IO.Writers
- Assembly
- Kampute.DocToolkit.dll
Definition
Represents a text writer that escapes HTML special characters and provides methods to write HTML elements.
public class HtmlWriter : MarkupWriter- Inheritance
Remarks
This class helps in writing Markdown content safely by escaping special characters where necessary. It extends the MarkupWriter class and provides additional methods tailored for HTML formatting. It ensures well-formed HTML output through several key features:
- Safety– Automatic HTML character escaping converts special characters to their corresponding HTML entities, preventing XSS vulnerabilities and ensuring valid HTML output.
- Basic API– Methods for creating both block-level and inline HTML elements, with specialized support for common elements like links, code blocks, and comments.
- Structural Validation– Internal stack tracking of open elements ensures proper nesting and prevents common HTML structural errors. The current number of open tags can be retrieved via the OpenTagCount property.
- Resource Management– Automatic cleanup on disposal ensures all open tags are properly closed, preventing incomplete HTML structures even in exceptional cases.
Examples
The following example demonstrates the key features of HtmlWriter:
using var writer = new HtmlDocumentWriter(Console.Out, leaveOpen: true);
// Write a heading
writer.WriteHeading(1, "Title");
writer.WtiteLine();
// Write a paragraph
writer.WriteParagraph(w =>
{
w.Write("This is a paragraph with ");
w.WriteStrong("bold");
w.Write(" text.");
});
writer.WtiteLine();
// Add a blockquote
writer.WriteBlockquote("This is a blockquote.");
writer.WtiteLine();
// Add a code block
writer.WriteCodeBlock(@"var greeting = ""Hello, World!"";", "csharp");
writer.WtiteLine();
// Add a link
writer.WriteLink(new Uri("https://example.com"), "Visit Example");This produces the following HTML:<h1>Title</h1>
<p>This is a paragraph with <strong>bold</strong> text.</p>
<blockquote>This is a blockquote.</blockquote>
<pre dir="ltr"><code class="language-csharp">var greeting = "Hello, World!";</code></pre>
<a href="https://example.com/">Visit Example</a>Thread Safety
Public static members of the type are guaranteed to be thread-safe. However, public instance members are not thread-safe.
Constructors
| HtmlWriter(TextWriter, bool) | Initializes a new instance of the HtmlWriter class. |
Properties
| OpenTagCount | Gets the number of block elements that are still open. |
Methods
| Attribute(string, string) | Returns an attribute with the specified name and value. |
| Dispose(bool) | Releases the unmanaged resources used by the HtmlWriter and optionally releases the managed resources. |
| Write(char) | Writes a character after escaping HTML special characters. |
| WriteAttribute(string, string) | Writes a tag attribute with an optional value. |
| WriteBlockquote(Action<MarkupWriter>) | Writes a blockquote using the provided delegate to write the content. |
| WriteCodeBlock(string, string) | Writes a code block. |
| WriteComment(string) | Writes an HTML comment. |
| WriteDocLink(Uri, Action<MarkupWriter>) | Writes a hyperlink to the documentation of a code element with the specified URL and delegate for writing the link text. |
| WriteEmphasis(Action<MarkupWriter>) | Writes an emphasis using the provided delegate to write the content. |
| WriteEndElement() | Writes the closing tag of the last open HTML element. |
| WriteHeading(int, Action<MarkupWriter>) | Writes a heading using the provided delegate to write the content. |
| WriteHorizontalRule() | Writes a horizontal rule. |
| WriteImage(Uri, string) | Writes an image using the specified URL and an optional title. |
| WriteInlineCode(string) | Writes a code span using the specified text. |
| WriteInlineElement(string, IEnumerable<KeyValuePair<string, string>>) | Writes an inline HTML element with the specified name and attributes. |
| WriteLink(Uri, Action<MarkupWriter>) | Writes a hyperlink. |
| WriteList(int, Action<MarkupWriter, int>, bool) | Writes a list using the provided delegate to write the list items. |
| WriteParagraph(Action<MarkupWriter>) | Writes a paragraph using the provided delegate to write the content. |
| WriteStartElement(string, IEnumerable<KeyValuePair<string, string>>) | Writes the opening tag of an HTML element with the specified name and attributes. |
| WriteStrong(Action<MarkupWriter>) | Writes a strong emphasis using the provided delegate to write the content. |
| WriteTable(IReadOnlyList<string>, int, Action<MarkupWriter, int, int>) | Writes a table using the provided delegate to write the content of each cell. |
