Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.3 KiB

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