PrettyMarkupFormatter
Represents the an HTML5 markup formatter with inserted intends.
using AngleSharp.Dom;
using System.Linq;
namespace AngleSharp.Html
{
public class PrettyMarkupFormatter : IMarkupFormatter
{
private string _intendString;
private string _newLineString;
private int _intendCount;
public string Indentation {
get {
return _intendString;
}
set {
_intendString = value;
}
}
public string NewLine {
get {
return _newLineString;
}
set {
_newLineString = value;
}
}
public PrettyMarkupFormatter()
{
_intendCount = 0;
_intendString = "\t";
_newLineString = "\n";
}
string IMarkupFormatter.Comment(IComment comment)
{
return IntendBefore(comment.PreviousSibling) + HtmlMarkupFormatter.Instance.Comment(comment) + NewLineAfter(comment.NextSibling);
}
string IMarkupFormatter.Doctype(IDocumentType doctype)
{
return IntendBefore(doctype.PreviousSibling) + HtmlMarkupFormatter.Instance.Doctype(doctype) + NewLineAfter(doctype.NextSibling);
}
string IMarkupFormatter.Processing(IProcessingInstruction processing)
{
return IntendBefore(processing.PreviousSibling) + HtmlMarkupFormatter.Instance.Processing(processing) + NewLineAfter(processing.NextSibling);
}
string IMarkupFormatter.Text(string text)
{
string text2 = text.Replace('\n', ' ');
return HtmlMarkupFormatter.Instance.Text(text2);
}
string IMarkupFormatter.OpenTag(IElement element, bool selfClosing)
{
string str = IntendBefore(element.PreviousSibling ?? element.Parent);
_intendCount++;
return str + HtmlMarkupFormatter.Instance.OpenTag(element, selfClosing) + NewLineAfter(element.FirstChild ?? element.NextSibling);
}
string IMarkupFormatter.CloseTag(IElement element, bool selfClosing)
{
_intendCount--;
return IntendBefore(element.LastChild ?? element.Parent) + HtmlMarkupFormatter.Instance.CloseTag(element, selfClosing) + NewLineAfter(element.NextSibling ?? element.Parent);
}
string IMarkupFormatter.Attribute(IAttr attribute)
{
return HtmlMarkupFormatter.Instance.Attribute(attribute);
}
private string NewLineAfter(INode node)
{
if (node != null && node.NodeType != NodeType.Text)
return _newLineString;
return string.Empty;
}
private string IntendBefore(INode node)
{
if (node != null && node.NodeType != NodeType.Text)
return string.Join(string.Empty, Enumerable.Repeat(_intendString, _intendCount));
return string.Empty;
}
}
}