The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTML::Make::Page - Automate making HTML <head> and <body>

SYNOPSIS

    use HTML::Make::Page 'make_page';
    my ($html, $body) = make_page (title => 'Personal Home Page', lang => 'en');
    $body->add_text (<<EOF);
    <img src='under-construction.gif' alt='This site is ALWAYS under construction!!!'>
    <p>Personal details</p>
    EOF
    print $html->text ();

produces output

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>Personal Home Page</title>
    </head>
    <body>
    <img src='under-construction.gif' alt='This site is ALWAYS under construction!!!'>
    <p>Personal details</p>
    </body>
    </html>

(This example is included as synopsis.pl in the distribution.)

VERSION

This documents version 0.04 of HTML-Make-Page corresponding to git commit a4b30a2e24715e722e5ae536ede7e8bab77facf8 released on Mon Jul 31 07:13:34 2023 +0900.

DESCRIPTION

This module simplifies generating the <head> and <body> elements of an HTML page using HTML::Make.

Restrictions

The output is restricted to the following variants of HTML:

HTML 5 only

The output has a doctype of <!DOCTYPE html>.

Lower-case tags only

No <HTML> etc., the tags generated are lower-case only.

UTF-8 encoding only

The output has <meta charset='UTF-8'> added automatically.

Viewport is added automatically

The viewport meta element, necessary for viewing web pages on mobile phones, is automatically added to the output.

XHTML tags are not supported

XHTML tags of the form <br/> are not necessary in HTML 5 and are not supported.

FUNCTIONS

make_page

    my ($html, $body) = make_page (title => 'My Cool Page');

The argument to the function is a hash of options of the form make_page (%options). The options are as follows.

Options

css

An array reference giving a list of style sheets to be added using <link> in <head>.

    my ($h, $b) = make_page (css => ['my.css', 'your.css']);
js

An array reference of names of JavaScript files you want to include in your header. To add async or defer to the script tag, use a hash reference in place of the name of the script with the keys src for the file and then any true value for async or defer to add those.

To add JavaScript as text, use a field text.

    my ($h, $b) = make_page (js => ['cat.js', 'dog.js', {src => 'parrot.js', async => 1}]);
lang

The language of your page, for example "en" for English. This is added to the opening <html> tag.

    my ($h, $b) = make_page (lang => "en");

Link elements. The value must be an array reference containing hash references. Each hash reference must contain the compulsory rel key, otherwise it is not included in the output.

If no href is present, a warning is printed. This warning may be turned off with "quiet".

CSS stylesheets can be added using this, but they can also be added more simply using the "css" option.

    my ($h, $b) = make_page (link => [{rel=>"icon", type=>"image/png", href=>"favicon.png"}]);
meta

Meta-tag things which you want to add, for example author, description or keywords meta-tags. The value must be an array reference containing hash references.

Note that the viewport meta tag and the charset meta tag are included by default.

    my ($h, $b) = make_page (meta => [{name => 'author', content => 'Long John Silver'}]);
quiet

Suppress warnings about harmless things like omitted titles and unknown options.

    my ($h, $b) = make_page (quiet => 1);
style

CSS in text format. The starting and closing <style> html tags will be added for you.

    my $style = <<EOF;
h1 {
   color: white;
   background-color: #FFF;
}
EOF
    my ($h, $b) = make_page (style => $style);
title

The title of your page. A <title> element is "compulsory" in HTML so if you omit this a warning is printed. Suppress the warning with "quiet".

    my ($h, $b) = make_page (title => "My Cool Page");

Examples

Inline JavaScript in the header

This puts JavaScript inline into the header and then calls the JavaScript using onload on the <body> HTML element.

    use utf8;
    use HTML::Make::Page 'make_page';
    my $jstext = <<EOF;
    function love() {
        alert ("💕 I love you baby 🥰");
    }
    EOF
    my ($h, $b) = make_page (js => [{text => $jstext}], title => '💌');
    $b->add_attr (onload => 'love ();');
    print $h->text ();

produces output

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <script>
    function love() {
        alert ("💕 I love you baby 🥰");
    }
    </script>
    <title>💌</title>
    </head>
    <body onload="love ();">
    </body>
    </html>

(This example is included as js-text.pl in the distribution.)

DEPENDENCIES

HTML::Make

This module is based on HTML::Make.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

This package and associated files are copyright (C) 2021-2023 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.