Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Robust HTML Tidy/Beautify/ PrettyPrinter

Status
Not open for further replies.

MasterRacker

New member
Oct 13, 1999
3,343
US
Does anyone know of a really solid (free) beautifier for HTML?

I was using 1stPage from EvrSoft and it's built-in tidy until I found it stripped quotes from click-events etc. thereby breaking the code.

I've been using HTML-Kit for a while now and I absolutely love the editing interface. It's built-in tidy is the tidy available through w3c.org. It works beautifully with standard HTML. Unfortunately, it has even worse problems with scripts. I've had a number of instances where a script was completely mangled and corrupted.

Anyone know of anything out there? I just want to get nice, clean, consistent indentation and "prettified" readable output.


Jeff
If your mind is too open your brains will fall out...
 
I use , but I think it's the same underlying code that you've already had problems with.

Are you sure that your scripts are properly incorporated into the HTML page? Does the page validate?

I think it's a good idea, where possible, to push your scripts to external files like this:
[tt]
<script type=&quot;text/javascript&quot; src=&quot;/js/nav_to.js&quot;></script>
[/tt]
That way they get cached, and can be shared between pages. It also saves the code from being misinterpreted by prettifiers and validators.


-- Chris Hunt
 
I would normally put anything I intended to re-use into an external include, however a specific validation or something I would put right in the file.

Here's a small sample I whipped up that validates correctly through the W3C online service (allowing for TT's line wrapping in posts)
----------------------------------------------------
[red]
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=US-ASCII&quot; >
    <title>Tidy Test</title>
    <script type=&quot;text/javascript&quot;>
<!--
  function validNum(num)
{
  num = parseInt(num);
  if ((num < 3) || (num > 7))
  {
    alert(&quot;Number out of range (3-7)&quot;);
    window.focus();
    document.frmTest.txtNum.select(); 
  }
}
// -->
    </script>
  </head>
  <!-- *************** BODY *************** -->
  <body style=&quot;font-family: sans-serif; font-size: 10pt;&quot; onload=&quot;document.frmTest.txtNum.select();&quot;>
    <form action=&quot;post&quot; name=&quot;frmTest&quot;>
      Enter a number from 3 to 7: <input type=&quot;text&quot; name=&quot;txtNum&quot; value=&quot;0&quot;>
      <br>
       <button type=&quot;button&quot; name=&quot;btnValidate&quot; onclick=&quot;validNum(document.frmTest.txtNum.value)&quot;>Validate</button>
    </form>
  </body>
</html>
[/red]

If the first check in the validNum function is &quot;num = 3&quot;
the tidy works fine. If it is &quot;num < 3&quot; however, it blows hugely. It throws the error [green]'<'+'/'+ letter not allowed here[/green]. It then splits the script after &quot;num&quot; and places the rest of it inside the body tag and throws a bunch of cascading errors from that result. Nasty.

This is a very simple script that you would think any tidy should handle. There may be some subtle setting in the tidy I can adjust, but I sure can't find it.

The link you gave is the same utility and it's doing essentially the same thing.


Jeff
If your mind is too open your brains will fall out...
 
It sounds to me as if you're looking for something that checks your scripting logic. When you find one that works, buy all the rights to it and you'll be richer than Bill Gates!

HTML code is pretty cut-and-dried as far as &quot;Tidy&quot; programs go. It's relatively easy to check tags and their placement. But when it comes to scripting or programming logic, there's no &quot;right&quot; way to do it as long as the net result is what you wanted. Yes, some methods are better than others and some scripts are faster than others but a 100 line script that adds 2 numbers together is as viable as a 1 line script that does the same thing. Give 10 programmers a specific problem to solve and you'll get 10 different programs that all do the same thing.

I'm sure that there's a &quot;Tidy&quot; program for a scripts out there but at best it's going to limit your scripting creativity by keeping you in a very narrow area of scripting logic.



There's always a better way. The fun is trying to find it!
 
I'm not talking about checking or adjusting logic at all.

I'm simply looking to perform source code beautification, for the most part, cleaning up indentation, so that you don't have to worry about it when typing in your code. The other use, of course, is &quot;compressing&quot; a page by removing whitespace to shrink it's download size and being able to take a compressed page and return it to something humanly readable.

The sample page validates correctly thru the W3C validator. The HTMLTidy on the same W3C site kills it. That's not good. At worst, a tidy should ignore anything inside HTML comments while prettifying the rest of the page.


Jeff
If your mind is too open your brains will fall out...
 
Looks like Tidy is broken: it should ignore stuff inside comments <!-- and --> just like a browser IMO.

(or it should have a setting to NOT TIDY what's in the comments)

So, since your script is correctly hidden inside HTML comments it would tidy everything else nicely (you probably have nicely indented script already anyway).

Email the folks who make/made tidy and/or html-kit and find out what they think :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
I pulled the script, tidied the page and put the script back in. ;-)

Problems similar to this one are listed in the support forums for tidy from about 2 years ago. There was a mention of a beta version of a new tidy but the link has been removed. It doesn't appear that anyone is working on it at the moment.

That is the same case as 1stPage. The errors in it's built in tidy were already mentioned in their support forums, also from a couple of years ago. Evrsoft has redesigned their website and is getting ready to release a version 3 of the editor, but it appears that they're not working on version 2 at all. Also, they've added a currently non-functioning payment option to their site, so I'm guessing that they want to try selling version 3.

I had the same types of problems with early versions of FrontPage. It would automatically reformat a page when you loaded it, but the re-formatting frequently broke pre-made scripts.

I'm just posting here to see if anyone out there has had success with any other tools on something more than basic HTML.


Jeff
If your mind is too open your brains will fall out...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top