home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / HTML / AsSubs.pm next >
Encoding:
Perl POD Document  |  1996-08-01  |  2.3 KB  |  103 lines  |  [TEXT/McPL]

  1. package HTML::AsSubs;
  2.  
  3. =head1 NAME
  4.  
  5. HTML::AsSubs - functions that construct a HTML syntax tree
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.  use HTML::AsSubs;
  10.  $h = body(
  11.        h1("This is the heading"),
  12.        p("This is the first paragraph which contains a ",
  13.          a({href=>'link.html'}, "link"),
  14.          " and an ",
  15.          img({src=>'img.gif', alt=>'image'}),
  16.          "."
  17.         ),
  18.       );
  19.  print $h->as_HTML;
  20.  
  21. =head1 DESCRIPTION
  22.  
  23. This module exports functions that can be used to construct various
  24. HTML elements. The functions are named after the tags of the
  25. correponding HTML element and are all written in lower case. If the
  26. first argument is a I<hash> then it will be used to initialize the
  27. attributes of this element. The remaining arguments are regarded as
  28. content.
  29.  
  30. =head1 ACKNOWLEDGEMENT
  31.  
  32. This module was inspired by the following message:
  33.  
  34.  Date: Tue, 4 Oct 1994 16:11:30 +0100
  35.  Subject: Wow! I have a large lightbulb above my head!
  36.  
  37.  Take a moment to consider these lines:
  38.  
  39.  %OVERLOAD=( '""' => sub { join("", @{$_[0]}) } );
  40.  
  41.  sub html { my($type)=shift; bless ["<$type>", @_, "</$type>"]; }
  42.  
  43.  :-)  I *love* Perl 5!  Thankyou Larry and Ilya.
  44.  
  45.  Regards,
  46.  Tim Bunce.
  47.  
  48.  p.s. If you didn't get it, think about recursive data types: html(html())
  49.  p.p.s. I'll turn this into a much more practical example in a day or two.
  50.  p.p.p.s. It's a pity that overloads are not inherited. Is this a bug?
  51.  
  52. =head1 BUGS
  53.  
  54. The exported link() function overrides the builtin link() function.
  55. The exported tr() function must be called using &tr(...) syntax
  56. because it clashes with the builtin tr/../../ operator.
  57.  
  58.  
  59. =head1 SEE ALSO
  60.  
  61. L<HTML::Element>
  62.  
  63. =cut
  64.  
  65. require HTML::Element;
  66. require Exporter;
  67. @ISA = qw(Exporter);
  68.  
  69. @TAGS = qw(html
  70.        head title base link meta isindex nextid
  71.        body h1 h2 h3 h4 h5 h6 p pre address blockquote
  72.        a img br hr
  73.        ol ul dir menu li
  74.        dl dt dd
  75.        cite code em kbd samp strong var
  76.        b i u tt
  77.        table tr td th caption
  78.        form input select option textarea
  79.       );
  80.  
  81. for (@TAGS) {
  82.     push(@code, "sub $_ { _elem('$_', \@_); }\n");
  83.     push(@EXPORT, $_);
  84. }
  85. eval join('', @code);
  86. if ($@) {
  87.     die $@;
  88. }
  89.  
  90. sub _elem
  91. {
  92.     my $tag = shift;
  93.     my $attributes;
  94.     if (@_ and defined $_[0] and ref($_[0]) eq "HASH") {
  95.     $attributes = shift;
  96.     }
  97.     my $elem = new HTML::Element $tag, %$attributes;
  98.     $elem->push_content(@_);
  99.     $elem;
  100. }
  101.  
  102. 1;
  103.