# Beatify html?

**URL:** <https://www.ruby-forum.com/t/beatify-html/92957>\
**Category:** Rails\
**Created:** [April 26, 2007, 1:46am UTC](https://www.ruby-forum.com/t/beatify-html/92957 "2007-04-26T01:46:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 26, 2007, 1:46am UTC](https://www.ruby-forum.com/t/beatify-html/92957/1 "2007-04-26T01:46:42Z")

</div>

Is there any way to beatify the html that is emitted? Some of the  
html that I generate dynamically is poorly formatted, and it would be  
somewhat difficult to make it pretty (particularly in the context of  
the static html that surrounds it). It there a mechanism by which I  
can (for debugging purposes at least) beatify the final html?

For example, is there a hook that allows one to intercept the  
generated html and apply final processing before emitting it?

Or perhaps there is an option in the Webrick server to beatify the  
output?

Or perhaps something else?

Thanks,  
Alex

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 26, 2007, 5:50am UTC](https://www.ruby-forum.com/t/beatify-html/92957/2 "2007-04-26T05:50:55Z")

</div>

alex wrote:

> Is there any way to beatify the html that is emitted?

Get Tidy, and run this assertion from your functional tests:

def assert\_tidy(messy = @response.body, verbosity = :noisy)  
scratch\_html = RAILS\_ROOT + ‘/…/scratch.html’ # TODO tune me!  
File.open(scratch\_html, ‘w’){|f| f.write(messy) }  
gripes = `tidy -eq #{scratch_html} 2>&1`  
gripes.split(“\n”)  
exclude, inclued = gripes.partition do |g|  
g =~ / - Info: / or  
g =~ /Warning: missing \<!DOCTYPE\> declaration/ or  
g =~ /proprietary attribute/ or  
g =~ /lacks “(summary|alt)” attribute/  
end  
puts inclued if verbosity == :noisy  
# inclued.map{|i| puts Regexp.escape(i) }  
assert\_xml `tidy -wrap 1001 -asxhtml #{scratch_html} 2>/dev/null`  
# CONSIDER that should report serious HTML deformities  
end

You can take out the assert\_xml if you don’t have yar\_wiki (whence that  
comes).

The system will push a scratch.html file into your parent folder, and if  
the  
errors Tidy reports are not utterly trivial, it will barf them out when  
your  
tests run.

When upgrading old websites to Rails I always run things through  
Tidy -i -asxhtml, before inserting the \<%%\> tags. The difference in  
development speed is astounding, when I don’t need to count the freakin’  
tags just to nest everything correctly.

My Short Cut with O’Reilly has lots more tips on keeping the HTML clean.

```
http://www.oreilly.com/catalog/9780596510657

```

–  
Phlip  
["Pigleg too?", week 1](http://flea.sourceforge.net/PiglegToo_1.html)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 26, 2007, 5:51am UTC](https://www.ruby-forum.com/t/beatify-html/92957/3 "2007-04-26T05:51:55Z")

</div>

You could switch to markaby which makes you write everything in Ruby  
(akin to XmlBuilder) and emits pretty HTML (I’m pretty sure).

[http://redhanded.hobix.com/inspect/markabyForRails.html](http://redhanded.hobix.com/inspect/markabyForRails.html)

Having “beautiful” HTML code means very little for a user of your site  
though…

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 27, 2007, 12:50am UTC](https://www.ruby-forum.com/t/beatify-html/92957/4 "2007-04-27T00:50:47Z")

</div>

Thanks to everybody for the helpful responses.

> Having “beautiful” HTML code means very little for a user of your site  
> though…

It is useful to me when I examine my output.

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 27, 2007, 12:08am UTC](https://www.ruby-forum.com/t/beatify-html/92957/5 "2007-04-27T00:08:44Z")

</div>

haml will give you perfect html output.

[http://haml.hamptoncatlin.com/](http://haml.hamptoncatlin.com/)

/Dustin
