4

I'm wondering if there's any kind of tool out there to Tidy Html on the fly.

Currently in my app, I use a MasterPage and then my Views are loaded into the Masterpage. The problem is that the <asp:content runat="server" ... /> always adds additional whitespace/linebreaks in the output HTML.

What I'd really like to do is clean it up so that

<title>
    This is my title

</title>

will look like

<title>This is my title</title>

Now I realize that I can go through and set

<asp:content ID="Content1" runat="server" ContentPlaceHolderID="TitleContent">This is my title</asp:content>

But that becomes a pain because I often use Ctrl + k + d which results in the reformatting that is undesirable.

Moreover, when I use inline code like

 <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">User <%: Model.UserName%> - Urban Now</asp:Content>

and use the "reformatting" key strokes, then there are also line breaks before and after the <%: Model.UserName%> and there is no way to set that formatting in the "Tools/Options/Formatting --> Tag Specific Options" (at least not that I can find).

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • 1
    Off topic, but I really liked to see the keyboard-like keys in your question. I'll start to use them from now on... – Leniel Maccaferri Jul 25 '10 at 01:52
  • 2
    I use `Ctrl + k + d` – Chase Florell Jul 25 '10 at 01:58
  • 1
    don't over use them though, or they might get banned :S – Chase Florell Jul 25 '10 at 01:58
  • Do you feel like the CPU of the server your site will run on is underutilized? I mean I completely understand wanting clean HTML from a conceptual point of view, but realistically, almost no one will see it, and if you gzip your html before sending it out, the white spaces should be compressed out for transmission and probably won't affect your bandwidth usage. – Roman Jul 16 '11 at 14:38

1 Answers1

1

TidyManaged is a managed .NET/Mono wrapper for the open source, cross-platform Tidy library, a HTML/XHTML/XML markup parser & cleaner originally created by Dave Raggett.

Sample Usage

using System;
using TidyManaged;

public class Test
{
  public static void Main(string[] args)
  {
    using (Document doc = Document.FromString("<hTml><title>test</tootle><body>asd</body>"))
    {
      doc.ShowWarnings = false;
      doc.Quiet = true;
      doc.OutputXhtml = true;
      doc.CleanAndRepair();
      string parsed = doc.Save();
      Console.WriteLine(parsed);
    }
  }
}

results in:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 13), see www.w3.org" />
<title>test</title>
</head>
<body>
asd
</body>
</html>

Please note that <title>test</tootle> is changed to the correct <title>test</title>.

https://github.com/markbeaton/TidyManaged

Frank van Eykelen
  • 1,926
  • 1
  • 23
  • 31