IDocumentFormatter.CreateMinifier Method

Namespace
Kampute.DocToolkit.Formatters
Assembly
  • Kampute.DocToolkit.dll

Definition

Creates a TextWriter instance that wraps the specified writer to minimize the content for the target format.
TextWriter CreateMinifier(TextWriter writer)

Parameters

writer TextWriter
The TextWriter to write the minimized content to.

Returns

TextWriter
A new instance of TextWriter that wraps the specified writer for minimizing the content, or the original writer if the target format does not support minimization.

Remarks

This factory method creates a specialized writer that optimizes the output by removing unnecessary whitespace and formatting while preserving the semantic meaning of the content. The optimization is format-specific and may include:
  • Removing redundant spaces and line breaks
  • Compressing consecutive whitespace characters
  • Preserving required whitespace in code blocks and other sensitive areas

If the target format supports minimization, this method returns a new TextWriter that wraps the specified writer. The wrapper instance takes ownership of the specified writer and will dispose it when the wrapper is disposed. If the target format does not support minimization, the original writer is returned.

After calling this method, the caller should not write to, close, or dispose the original writer directly to ensure that the minimized content is written correctly.
It is the caller's responsibility to dispose the returned MarkupWriter instance to release any resources.

Examples

Here is an example of how to use the IDocumentFormatter.CreateMinifier(TextWriter) method:
using var minifiedWriter = formatter.CreateMinifier(textWriter);
using var markupWriter = formatter.CreateMarkupWriter(minifiedWriter);
markupWriter.WriteHeading("Title", 1);
markupWriter.WriteParagraph("This is properly encoded content for the target format.");