title: C# Tabify/Indent HTML
body: <p>Is there a tool/library/function in C# which tabifies or indents generated html code without validating or tidying the input?</p>
  
  <p>Edit:</p>
  
  <p>Indent generated HTML code from JavaScript TextEditors, including but not limited to TinyMCE. No HtmlTextWriter. Must not expect a valid XML/XHTML/HTML code.</p>
  
  <p>Requirement:</p>
  
  <ul>
  <li>Add a new line before and after opening and closing tags.</li>
  <li>Indent content inside tags (Tab or 4 Spaces).</li>
  <li>Split a long line (having N number of words) into multiple indented lines.</li>
  <li>Do not change the input even though it is not a valid HTML. Only tabify/indent and split long lines.</li>
  </ul>
  
  <p>Upto this point, I have:</p>
  
  <pre><code>private string FormatHtml(string input)
  {
      //Opening tags
      Regex r = new Regex("&lt;([a-z]+) *[^/]*?&gt;");
      string retVal = string.Empty;
      retVal = r.Replace(input, string.Format("$&amp;{0}\t", Environment.NewLine));
  
      //Closing tags
      r = new Regex("&lt;/[^&gt;]*&gt;");
      retVal = r.Replace(retVal, string.Format("{0}$&amp;{0}", Environment.NewLine));
  
      //Self closing tags
      r = new Regex("&lt;[^&gt;/]*/&gt;");
      retVal = r.Replace(retVal, string.Format("$&amp;{0}", Environment.NewLine));
  
      return retVal;
  }
  </code></pre>