#!/usr/bin/perl

use strict;
use warnings;
use XML::Twig;
use Regexp::Common 'URI';

my $PLANT_FILE="plant.xml";

my $twig= XML::Twig->new(             # $_ is the element
  twig_handlers => { common    => sub { $_->set_tag( 'h1') },
                     botanical => sub { $_->set_tag_class( 'p') },
                     zone      => sub { $_->set_tag_class( 'p');
                                        $_->prefix( "Grows in zone ");
                                        $_->suffix( ".");
                                      },
                     light     => sub { $_->set_tag_class( 'p');
                                        $_->prefix( "Required Light: ");
                                        $_->suffix( ".");
                                      },
                     price     => sub { $_->set_tag_class( 'p'); },
                     available => sub { $_->set_tag_class( 'span');
                                        $_->prefix( ", available ");
                                        $_->suffix( ".");
                                        $_->move( last_child => $_->prev_sibling);
                                      },
                    desc       => sub { $_->set_tag_class( 'p');
                                        $_->insert_new_elt( before => h2 => "Description");
                                        $_->subs_text( qr/($RE{URI}{HTTP})/, 
                                                      '&elt( a =>{ href => $1 }, $1)'
                                                 );
                                      },
                    plant      => sub { $_->set_tag( 'body'); },
                   },
  pretty_print => 'indented',
                        );

$twig->parsefile( $PLANT_FILE);

# add the html "wrapping"
my $html= $twig->root->wrap_in( 'html');
my $head= $html->insert_new_elt( first_child => 'head');
my $name= $twig->first_elt( 'h1')->text;
$head->insert_new_elt( first_child => 'title', $name);

$twig->print;
