SnowWolf75 has asked for the wisdom of the Perl Monks concerning the following question:

I would like to generate some HTML code using HTML::Tiny (because it's simple and lightweight), but it doesn't seem to do proper indenting... even on the example code given in the module. Their example:
use HTML::Tiny; my $h = HTML::Tiny->new; # Generate a simple page print $h->html( [ $h->head( $h->title( 'Sample page' ) ), $h->body( [ $h->h1( { class => 'main' }, 'Sample page' ), $h->p( 'Hello, World', { class => 'detail' }, 'Second para' ) ] ) ] );
produces:
<html><head><title>Sample page</title></head> <body><h1 class="main">Sample page</h1><p>Hello, World</p> <p class="detail">Second para</p> </body> </html>
instead of the neatly formatted example they show. Did I miss a flag somewhere in the creation/use of it?

Replies are listed 'Best First'.
Re: Properly indented using HTML::Tiny
by planetscape (Chancellor) on Oct 20, 2009 at 04:24 UTC

    Are you, by chance, looking for HTML::Tidy?

    HTH,

    planetscape
      No, because it seems meant to take a config file and output formatted text, not create said formatted text on the fly. Google kept suggesting it too, so I guess it's a popular/common mistake. btw, I like your multicolor signature.
Re: Properly indented using HTML::Tiny
by gmargo (Hermit) on Oct 20, 2009 at 00:22 UTC

    In the past I have used CGI::Pretty (part of CGI) to do something similar. Very useful when debugging tables. Not exactly what you're asking for though.

    This CGI::Pretty code more-or-less duplicates your HTML::Tiny sample code:

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use CGI::Pretty qw(-oldstyle_urls -no_xhtml); my $q = CGI::Pretty->new(); print $q->start_html(-title => 'Sample page'); print $q->h1({-class => 'main'}, 'Sample page'); print $q->p('Hello, World'); print $q->p({-class => 'detail' }, 'Second para' ); print $q->end_html();
    Output is:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en-US"><head><title>Sample page</title> </head> <body> <h1 class="main"> Sample page </h1> <p> Hello, World </p> <p class="detail"> Second para </p> </body> </html>

Re: Properly indented using HTML::Tiny
by ikegami (Patriarch) on Oct 19, 2009 at 22:59 UTC

    but it doesn't seem to do proper indenting

    Good. I'd be annoyed if it added format-changing spaces such as the one you describe. I hate to see links that protrude and cells that are extended, so I wouldn't want my HTML generator to produce the errors you desire.

    That goes to explain why it doesn't indent by default. I don't know if it has an option to do it on request.

Re: Properly indented using HTML::Tiny
by herveus (Prior) on Oct 20, 2009 at 11:59 UTC
    Howdy!

    What do *you* mean when you say "properly indented"? The output you present appears to have line breaks in appropriate places, but even those are not a formal requirement.

    You allude to an example in the POD; it would be helpful if you had included it to show us what you were expecting.

    Perhaps the lack of indentation is part and parcel of being "simple and lightweight".

    yours,
    Michael
      When I started my foray into coding, I picked up from somewhere that tags should be indented with a tree-like structure for readability. The output of the code snippet I allude to ( http://search.cpan.org/dist/HTML-Tiny/lib/HTML/Tiny.pm ) shows such indenting - tags that enclose other tags create an indent while sibling tags are at the same indent level - which is where my confusion and/or frustration came from. To me, the phrasing in the POD said I would get that indenting.

      The solution I found was to write my own functions - ti(), to(), and te() - to indent, 'out-dent', and 'equal-dent' the output by means of a global indent level variable. Putting these at the beginning of the line, or sometimes inside the HTML::Tiny autotags, created a nice flowing structure, at least to me. I know that there aren't hard standards for writing the HTML, but I prefer legibility over compactness more times. (likely why I've never gotten the hang of reading/writing obfuscated code)
Re: Properly indented using HTML::Tiny
by eye (Chaplain) on Oct 20, 2009 at 07:54 UTC
    ...instead of the neatly formatted example they show.

    While the first example is "pretty," most of the others are not. Close paragraphs abut the following open paragraph. The only other formatted output is preceded by

    ...which would print the unformatted version of:

    The formatting you see appears to be added for clarity, not accuracy.