Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
html_fmt -- HTML Reformatter

NAME

html_fmt - Reformat HTML, indented according to structure

Synopsis

html_fmt [--avoid-whitespace=[yes|comment]]
  [--ws-ok-before-end-tag|--no-ws-ok-before-end-tag] [uri|file]

Description

Writes its input to STDOUT, reformatted and indented according to the HTML structure. With no arguments, html_fmt looks for its input on STDIN. If it has an argument that looks like a URI, html_fmt treats that argument as a URI. If it has an argument that does not look like a URI, html_fmt uses that argument as the name of its input file. html_fmt considers its argument to "look like a URI", if it starts with alphanumerics followed by a colon.

When reformatting and indenting, html_fmt takes measures to avoid introducing whitespace that will affect the way in which the HTML displays. The standards differ on whitespace treatment; when the standards agree they often allow considerable latitude to the browsers; and specific implementions will exploit the allowed latitude in different ways, or exceed it. All of which is to say that html_fmt's attempts to avoid introducing whitespace are not always successful.

html_fmt supplies missing start and end tags. If html_fmt cannot find required start and end tags, it supplies them. html_fmt's grammar is very liberal in its interpretation of what is valid HTML. Finally, if this is not sufficient to turn the input into valid HTML, html_fmt will treat problem sections of the input as noise or "cruft", ignoring them in determining the structure of the document. html_fmt never rejects its input, whatever its contents.

When html_fmt adds a missing start tag, it follows the new start tag with a comment. When html_fmt adds a missing end tag, it preceeds the new end tag with a comment. When html_fmt classifies characters as "cruft", it adds a comment to that effect before the "cruft".

pre and script elements receive special treatment. The contents of pre and script elements are not reformatted. When missing tags or cruft occur inside a pre element, the comments to that effect are placed before the <pre> start tag. The contents of script elements are not examined.

Options

No single set of reformatting choices is anywhere near adequate to the variety of standards, renderers and application requirements out there. Ideally, html_fmt would have many options customizing its behavior. As of this writing, there are only two.

avoid-whitespace

The --avoid-whitespace option may take one of two values: yes and comment. If the value is yes, html_fmt does not try to add indentation in whitespace-sensitive places. This avoids clutter but, because of the missed indentation, the structure of the document becomes harder to follow. yes is the default value.

If the value is comment, HTML (SGML) comments are used to achieve proper indentation without introducing whitespace. This works, but does get a bit cluttered.

ws-ok-before-end-tag

The --ws-ok-before-end-tag option is a Boolean. If set, html_fmt will add whitespace before an end tag when that is useful. If the SGML standards are adhered to, the added whitespace will not be rendered. However, the HTML standards, while they allow SGML-adherent behavior for whitespace before end tags, do not require it. In practice, many browsers will render whitespace found before end tags.

If --ws-ok-before-end-tag is false, html_fmt will not add whitespace before end tags. --ws-ok-before-end-tag is false by default. This is always safe, in the sense that it will not change what is there to be rendered. But it limits the ability of html_fmt to make its output readable.

Example

Given this input:

<title>Test page<tr>x<head attr="I am cruft"><p>Final graf

to this command:

html_fmt --avoid-whitespace=comment --ws-ok-before-end-tag

the output is

<html>
  <!-- html_fmt: Preceeding start tag is replacement for a missing one -->
  <head>
    <!-- html_fmt: Preceeding start tag is replacement for a missing one -->
    <title>
        Test page
        <!-- html_fmt: Following end tag is replacement for a missing one -->
    </title>
    <!-- html_fmt: Following end tag is replacement for a missing one -->
  </head><!--
    html_fmt: this comment is to avoid introducing whitespace
  --><body>
    <!-- html_fmt: Preceeding start tag is replacement for a missing one -->
    <table>
        <!-- html_fmt: Preceeding start tag is replacement for a missing one -->
        <tbody>
          <!-- html_fmt: Preceeding start tag is replacement for a missing one -->
          <tr>
            <td>
              <!-- html_fmt: Preceeding start tag is replacement for a missing one -->
              x<!--
                html_fmt: this comment is to avoid introducing whitespace
              --><!-- html_fmt: Next line is cruft --><!--
                html_fmt: this comment is to avoid introducing whitespace
              --><head attr="I am cruft"><!--
                html_fmt: this comment is to avoid introducing whitespace
              --><p>
                Final graf
                <!-- html_fmt: Following end tag is replacement for a missing one -->
              </p>
              <!-- html_fmt: Following end tag is replacement for a missing one -->
            </td>
            <!-- html_fmt: Following end tag is replacement for a missing one -->
          </tr>
          <!-- html_fmt: Following end tag is replacement for a missing one -->
        </tbody>
        <!-- html_fmt: Following end tag is replacement for a missing one -->
    </table>
    <!-- html_fmt: Following end tag is replacement for a missing one -->
  </body>
  <!-- html_fmt: Following end tag is replacement for a missing one -->
</html>

Acknowledgements

The starting template for this code was HTML::TokeParser, by Gisle Aas.

LICENSE AND COPYRIGHT

Copyright 2007-2012 Jeffrey Kegler, all rights reserved. Marpa is free software under the Perl license. For details see the LICENSE file in the Marpa distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment