
=encoding UTF-8

=head1 NAME

HTML::Make - A flexible HTML generator

=head1 SYNOPSIS

    
    # Make a table.
    use HTML::Make;
    my $table = HTML::Make->new ('table');
    my $tr = $table->push ('tr');
    my $td = $tr->push ('td', text => 'This is your cell.');
    # Add HTML as text.
    $td->add_text ('<a href="http://www.example.org/">Example</a>');
    # Get the output
    print $table->text ();


outputs

    <table><tr><td>This is your cell.<a href="http://www.example.org/">Example</a></td>
    </tr>
    </table>


=head1 VERSION

This documents HTML::Make version 0.06 corresponding to git
commit L<9bd1ebbc0f95af57e6885dbde4025e8709ba9dd4|https://github.com/benkasminbullock/HTML-Make/commit/9bd1ebbc0f95af57e6885dbde4025e8709ba9dd4>
released on Mon Nov 28 13:37:29 2016 +0900.

=head1 DESCRIPTION

An HTML generator. Make a top node using L</new>

    my $top_node = HTML::Make->new ('ul');

then add child elements to the top node using L</push>:

    my $element = $top_node->push ('li');

Add text to elements using L</add_text>:

    $element->add_text ('Ça plane pour moi');

Add attributes to elements using L</add_attr>:

    $element->add_attr (class => 'plastic bertrand');

Get the HTML as text with L</text> on the top node:

    my $html = $top_node->text ();

Add HTML to an element using L</add_text>:

    $element->add_text ("<p>This is a paragraph with <i>italic</i> text.</p>");

=head1 METHODS

=head2 new

    my $element = HTML::Make->new ('li');

Make a new HTML element of the specified type.

It is possible to add attributes or text to the item. To add
attributes, use the following syntax:

    my $element = HTML::Make->new ('li', attr => {class => 'biglist'});

To add text,

    my $element = HTML::Make->new ('li', text => "White punks on dope");

Both attributes and text may be added:

    my $element = HTML::Make->new ('li', attr => {id => 'ok'}, text => 'OK');

HTML::Make uses the list of known HTML tags from
L</HTML::Valid::Tagset>, and will issue a warning if the type given as
the first argument to L</new> is not on its list of tags. To switch
off this behaviour, use the C<nocheck> option:

    my $freaky = HTML::Make->new ('freaky', nocheck => 1);

=head2 add_attr

    $obj->add_attr (class => 'buggles');

Add attributes to the specified object, in other words

    my $obj = HTML::Make->new ('li');
    $obj->add_attr (class => 'beano');
    my $obj_text = $obj->text ();
    # <li class="beano"></li>

This issues a warning of the form B<"Overwriting attribute 'class' for
'li'"> if the object already contains an attribute of the specified
type.

This issues a warning if the attribute is not valid for the tag,
according to the C<tag_attr_ok> method of
L</HTML::Valid::Tagset>. This is restricted to what is valid in HTML5.

=head2 add_text

    $element->add_text ('buggles');

Add text to C<$element>. For example,

    my $element = HTML::Make->new ('p');
    $element->add_text ('peanuts');
    print $element->text ();
    # <p>peanuts</p>

The text may contain HTML elements:

    my $element = HTML::Make->new ('p');
    $element->add_text ('peanuts <i>eggs</i>');
    print $element->text ();
    # <p>peanuts <i>eggs</i></p>

The return value is the added text object.

=head2 push

    my $child = $element->push ('tag');

Add child element of type <tag> to C<$element> and return the result
as a new C<HTML::Make> object. For example,

    my $table = HTML::Make->new ('table');
    my $row = $table->push ('tr');
    my $cell = $row->push ('td');
    print $table->text ();
    # <table><tr><td></td></tr></table>

It's also possible to add all of the same arguments as L</new>, for
example

    $element->push ('a', attr => {href => 'http://www.example.org/'});

This warns if you add some types of elements to possibly inappropriate
parent elements. For example if you add an <li> tag to a <tr> or
something.

=head2 opening_tag

    my $tag = $obj->opening_tag ();

Returns the text value of the HTML tag opening, complete with
attributes.

=head2 text

    $element->text ();

This function returns the element as text.

    my $p = HTML::Make->new ('p');
    print $p->text ();
    # <p></p>

=head2 multiply

    my @elements = $obj->multiply ('li', \@contents);

Create multiple child elements of C<$obj> of type given by the first
argument, with text contents given by C<\@contents>.

    
    use HTML::Make;
    my $ol = HTML::Make->new ('ol');
    $ol->multiply ('li', ['one', 'two', 'three']);
    print $ol->text ();


outputs

    <ol><li>one</li>
    <li>two</li>
    <li>three</li>
    </ol>


=head1 BUGS

This module assumes you want to make HTML5.

There is no way to control the whitespace in the output HTML.

There is no way to add HTML comments to the output except by adding
them as text.

=head1 DEPENDENCIES

This module depends on the following Perl modules.

=over

=item Carp

L<Carp> is used to report errors.

=item HTML::Valid::Tagset

L<HTML::Valid::Tagset> is used to validate tags and tag/attribute pairs.

=back

=head1 SEE ALSO

=over

=item L<CGI>

The CGI module contains HTML generation, although it is "deprecated".
Unfortunately very much more effort has gone into telling you about
why you should not use CGI's HTML generation than has gone into
suggesting what you should use instead.

=item L<HTML::Generator|http://cleancode.sourceforge.net/api/perl/HTML/Generator.html>

This project is on the SourceForge site, not CPAN. I have not tested
it.

=item L<HTML::Native>

=item L<HTML::TagTree>

=item L<HTML::Template>

=item L<HTML::Tiny>

This is similar to the HTML generation which is in L<CGI>. Its last
update, version 1.05, was in 2009, and it doesn't include HTML5 tags.

=back


=head1 AUTHOR

Ben Bullock, <bkb@cpan.org>

=head1 COPYRIGHT & LICENCE

This package and associated files are copyright (C) 
2012-2016
Ben Bullock.

You can use, copy, modify and redistribute this package and associated
files under the Perl Artistic Licence or the GNU General Public
Licence.



