Skip to content

shmem/html-writer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTML-Writer version 0.001
========================

WARNING: This module is very beta yet.

This module implements a DSL (domain specific language) for writing
(X)HTML files.

It parses a DTD file (default xhtml1-transitional.dtd, included in this
package) and sets up functions for entities and attributes found in the
DTD. Entity functions are all uppercase, while attribute functions have
a trailing underscore. Each function takes a code reference as its first
argument.

HTML::Writer works as a pragma. It calls the DTD generated functions only
if it is in effect in the current scope. To that end, it imports those
functions at compile time. Subroutines with conflicting names are saved
and restored with a 'no HTML::Writer' call.

After including HTML::Writer via use($dtd) - not via require - it is possible
to write HTML/XHTML/xml as if it were perl, with interspersed valid perl
code.

The first 'use' call loads HTML::Writer and makes it parse the passed DTD.
A subsequent 'use' call is needed to turn on HTML::Writer in the current
scope. Example:

    use HTML::Writer qw(xhtml1-transitional.dtd);
    my $foo = "very foo";
    my $bar = "bar foot";
    sub baz { "bazaar & cathedral" }
    no HTML::Writer;
    sub DIV { print "Divertimento finished\n"; }
    use HTML::Writer;
    render {
        HTML {
            HEAD {
                TITLE { "foo bar"};
            };
            BODY {
                class_ "ugly";
                onload_ "javascript: mumble()";
                DIV {
                    class_ "foo";
                    id_    "bar";
                    t      "If in doubt, mumble.";
                    # shape_ "bullet"; # <--- this would croak
                    IMG { src_ "foo.jpg" };
                };
                TABLE {
                    my $c;
                    for ($foo, $bar, baz) {
                        TR {
                            TD { $_ }; TD { $c++};
                        }
                    }
                };
                DIV { class_ "bar"; t "End of that." };
                A {
                    href_ "http://perlmonks.org";
                    t "Perl Monks Website";
                    name_ "PerlMonks";
                    title_ "PM";
                };
            }
        };
    };
    no HTML::Writer;
    DIV();

HTML::Writer croaks if attributes are found inside an entity block which
is illegal as per DTD.

The resulting HTML file is printed to STDOUT in void context, returned
as a string in scalar context, or returned as a list of lines in array
context. The above code produces:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
  <head>
    <title>foo bar</title>
  </head>
  <body class="ugly" onload="javascript: mumble()">
    <div class="foo" id="bar">If in doubt, mumble.
      <img src="foo.jpg"></img>
    </div>
    <table>
      <tr>
        <td>very foo</td>
        <td>0</td>
      </tr>
      <tr>
        <td>bar foot</td>
        <td>1</td>
      </tr>
      <tr>
        <td>bazaar &amp; cathedral</td>
        <td>2</td>
      </tr>
    </table>
    <div class="bar">End of that.</div>
    <a href="http://perlmonks.org" name="PerlMonks" title="PM">Perl Monks Website</a>
  </body>
</html>
Divertimento finished

INSTALLATION

To install this module type the following:

   perl Makefile.PL
   make
   make test
   make install

DEPENDENCIES

This module requires these other modules and libraries:

   XML::Writer
   XML::DTDParser
   LWP::Simple
   File::Slurp

COPYRIGHT AND LICENCE

Copyright (C) 2017 by Georg Moritz

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


About

write html/xml files in plain perl

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages