++ed by:
ADAMJS CJFIELDS DCPETROV DGINEV EGOR

18 PAUSE user(s)
7 non-PAUSE user(s).

Jeffrey Kegler

NAME

  html_fmt - Reformat HTML, indented according to structure

Synopsis

    html_fmt [--avoid-whitespace=[yes|comment]]
      [--no-ws-ok-before-end-tag|--no-ok-before-end-tag]
      [--added-tag-comment|--no-added-tag-comment]
      [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 a few.

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.

added-tag-comment

The --added-tag-comment option is a Boolean. If set, whenever html_fmt supplies a missing start or end tag, html_fmt will include a comment to that effect. If the boolean is unset with --no-added-tag-comment, html_fmt will be silent when supply missing start and end tags. --added-tag-comment is the default.

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.

Copyright and License

  Copyright 2012 Jeffrey Kegler
  This file is part of Marpa::R2.  Marpa::R2 is free software: you can
  redistribute it and/or modify it under the terms of the GNU Lesser
  General Public License as published by the Free Software Foundation,
  either version 3 of the License, or (at your option) any later version.

  Marpa::R2 is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser
  General Public License along with Marpa::R2.  If not, see
  http://www.gnu.org/licenses/.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 250:

=cut found outside a pod block. Skipping to next block.




Hosting generously
sponsored by Bytemark