NAME

Data::Formatter::Html - Perl extension for formatting data stored in scalars, hashes, and arrays into strings, definition lists, and bulletted lists, etc. using HTML.

SYNOPSIS

use Data::Formatter::Html;

my $text = new Data::Formatter::Html(\*STDOUT);
$text->out('The following foods are tasty:',
           ['Pizza', 'Pumpkin pie', 'Sweet-n-sour Pork']);

# Outputs,
#
# The following foods are tasty:
#  <ul>
#  <li> Pizza
#  <li> Pumpkin pie
#  <li> Sweet-n-sour Pork
#  </ul>
#

$text->out('Do these things to eat an orange:',
           \['Peal it', 'Split it', 'Eat it']);

# Outputs,
#
# Do these things to eat an orange: 
# <ol>
#  <li> Peal it 
#  <li> Split it 
#  <li> Eat it 
# </ol>
#

# If you don't need to output to a file, you can also use the format() class method
# instead of the out() instance method.
my $nums = Data::Formatter::Html->format(
    'Phone numbers' =>
     { 
         Pat => '123-4567',
         Joe => '999-9999',
         Xenu => '000-0000',
     }); 
       
# Stores in $nums:
#
# Phone numbers 
# <dl>
# <dt>Joe</dt><dd>999-9999</dd>
# <dt>Pat</dt><dd>123-4567</dd>
# <dt>Xenu</dt><dd>000-0000</dd>
# </dl>
#

DESCRIPTION

A module that converts Perl data structures into HTML code, formatting the data nicely for presentation to a human. For instance, refs to arrays are converted into bulleted lists, refs to arrays that contain only refs to arrays are converted into tables, and refs to hashes are converted to definition lists.

All in all, data structures are mapped to display elements as follows:

SCALAR                    => Text,
REF to an ARRAY of ARRAYs => Table
REF to an ARRAY           => Unordered (bulleted) list
Ref to a REF to an ARRAY  => Ordered (numbered) list
Ref to a HASH             => Definition list

Elements can be nested, so, for instance, you may have an array that contains one or more references to arrays, and it will be translated into a nested bulletted list.

Methods

PACKAGE->new()

Returns a newly created Data::Formatter::Html object.

PACKAGE->format(ARRAY)

Returns the string representation of the arguments, formatted nicely.

$OBJ->out(ARRAY)

Outputs the string representation of the arguments to the file stream specified in the constructor.

$OBJ->heading(SCALAR)

Returns a new data-structure containing the same data as SCALAR, but which will be displayed as a heading if passed to out(). Headings are center aligned, made all uppercase, and surrounded by a thick border.

For example,

	$text->out($text->heading("Test Results"), "All is well!");
 
$OBJ->emphasized(SCALAR)

Returns a new data-structure containing the same data as SCALAR, but which will be displayed as emphasized text if passed to out(). Emphasized text is made all uppercase and surrounded by a thin border.

For example,

$text->out($text->emphasized("Cannot find file!"));

Example

$formatter->out('Recipes',
    {
        "Zack's Kickin' Bannana Milkshake" =>
        [
            ['Ingredient', 'Amount', 'Preparation'],
            ['1% milk', '1 L',    ''],
            ['Ripe Banana', '2 peeled', \['Peel bananas', 'Chop into quarters for blender']],
            ['Organic eggs', '1 whole', \['Crack', 'Pour']],
            ['Wheat germ', '1 tablespoon', ''],
            ['Honey', 'To taste', 'Mix it in well!'],
        ],
        "Peanutbutter and Jam Sandwich" =>
        [
            ['Ingredient', 'Amount', 'Preparation'],
            ['Bread', '2 slices', ''],
            ['Jam', 'Enough to cover inner face of slice 1', ''],
            ['Peanutbutter', 'Enough to cover inner face of slice 2', '']
        ]
    }
);

The code above will output the HTML:

Recipes
Peanutbutter and Jam Sandwich
Ingredient Amount Preparation
Bread 2 slices
Jam Enough to cover inner face of slice 1
Peanutbutter Enough to cover inner face of slice 2
Zack's Kickin' Bannana Milkshake
Ingredient Amount Preparation
1% milk 1 L
Ripe Banana 2 peeled
  1. Peel bananas
  2. Chop into quarters for blender
Organic eggs 1 whole
  1. Crack
  2. Pour
Wheat germ 1 tablespoon
Honey To taste Mix it in well!

Note that the order of elements in a hash is not necessarily the same as the order the elements are printed in; instead, hash elements are sorted alphabetically by their keys before being output.

SEE ALSO

Data::Formatter::Text - A compatible module that outputs formatted information using ASCII text, rather than HTML.

AUTHOR

Zachary Blair, <zack_blair@hotmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Zachary Blair

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.