0.3
There's a lot of open issues
A long-lived project that still receives updates
A normaliser/beautifier for HTML that also understands embedded Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

HTML Beautifier

A normaliser/beautifier for HTML that also understands embedded Ruby. Ideal for tidying up Rails templates.

What it does

  • Normalises hard tabs to spaces (or vice versa)
  • Removes trailing spaces
  • Indents after opening HTML elements
  • Outdents before closing elements
  • Collapses multiple whitespace
  • Indents after block-opening embedded Ruby (if, do etc.)
  • Outdents before closing Ruby blocks
  • Outdents elsif and then indents again
  • Indents the left-hand margin of JavaScript and CSS blocks to match the indentation level of the code

Usage

From the command line

To update files in-place:

$ htmlbeautifier file1.html.erb [file2.html.erb ...]

or to operate on standard input and output:

$ htmlbeautifier < untidy.html.erb > formatted.html.erb

In your code

require 'htmlbeautifier'

beautiful = HtmlBeautifier.beautify(untify_html_string)

You can also specify how to indent (the default is two spaces):

beautiful = HtmlBeautifier.beautify(untidy_html_string, indent: "\t")

Installation

This is a Ruby gem. To install the command-line tool (you may need sudo):

$ gem install htmlbeautifier

To use the gem with Bundler, add to your Gemfile:

gem 'htmlbeautifier'

Contributing

  1. Follow these guidelines when writing commit messages (briefly, the first line should begin with a capital letter, use the imperative mood, be no more than 50 characters, and not end with a period).
  2. Include tests.