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.

235 lines
6.8 KiB

  1. package CGI::Pretty;
  2. # See the bottom of this file for the POD documentation. Search for the
  3. # string '=head'.
  4. # You can run this file through either pod2man or pod2html to produce pretty
  5. # documentation in manual or html file format (these utilities are part of the
  6. # Perl 5 distribution).
  7. use strict;
  8. use CGI ();
  9. $CGI::Pretty::VERSION = '1.05';
  10. $CGI::DefaultClass = __PACKAGE__;
  11. $CGI::Pretty::AutoloadClass = 'CGI';
  12. @CGI::Pretty::ISA = qw( CGI );
  13. initialize_globals();
  14. sub _prettyPrint {
  15. my $input = shift;
  16. foreach my $i ( @CGI::Pretty::AS_IS ) {
  17. if ( $$input =~ /<\/$i>/si ) {
  18. my ( $a, $b, $c, $d, $e ) = $$input =~ /(.*)<$i(\s?)(.*?)>(.*?)<\/$i>(.*)/si;
  19. _prettyPrint( \$a );
  20. _prettyPrint( \$e );
  21. $$input = "$a<$i$b$c>$d</$i>$e";
  22. return;
  23. }
  24. }
  25. $$input =~ s/$CGI::Pretty::LINEBREAK/$CGI::Pretty::LINEBREAK$CGI::Pretty::INDENT/g if $CGI::Pretty::LINEBREAK;
  26. }
  27. sub comment {
  28. my($self,@p) = CGI::self_or_CGI(@_);
  29. my $s = "@p";
  30. $s =~ s/$CGI::Pretty::LINEBREAK/$CGI::Pretty::LINEBREAK$CGI::Pretty::INDENT/g if $CGI::Pretty::LINEBREAK;
  31. return $self->SUPER::comment( "$CGI::Pretty::LINEBREAK$CGI::Pretty::INDENT$s$CGI::Pretty::LINEBREAK" ) . $CGI::Pretty::LINEBREAK;
  32. }
  33. sub _make_tag_func {
  34. my ($self,$tagname) = @_;
  35. return $self->SUPER::_make_tag_func($tagname) if $tagname=~/^(start|end)_/;
  36. # As Lincoln as noted, the last else clause is VERY hairy, and it
  37. # took me a while to figure out what I was trying to do.
  38. # What it does is look for tags that shouldn't be indented (e.g. PRE)
  39. # and makes sure that when we nest tags, those tags don't get
  40. # indented.
  41. # For an example, try print td( pre( "hello\nworld" ) );
  42. # If we didn't care about stuff like that, the code would be
  43. # MUCH simpler. BTW: I won't claim to be a regular expression
  44. # guru, so if anybody wants to contribute something that would
  45. # be quicker, easier to read, etc, I would be more than
  46. # willing to put it in - Brian
  47. return qq{
  48. sub $tagname {
  49. # handle various cases in which we're called
  50. # most of this bizarre stuff is to avoid -w errors
  51. shift if \$_[0] &&
  52. (ref(\$_[0]) &&
  53. (substr(ref(\$_[0]),0,3) eq 'CGI' ||
  54. UNIVERSAL::isa(\$_[0],'CGI')));
  55. my(\$attr) = '';
  56. if (ref(\$_[0]) && ref(\$_[0]) eq 'HASH') {
  57. my(\@attr) = make_attributes(shift);
  58. \$attr = " \@attr" if \@attr;
  59. }
  60. my(\$tag,\$untag) = ("\L<$tagname\E\$attr>","\L</$tagname>\E");
  61. return \$tag unless \@_;
  62. my \@result;
  63. my \$NON_PRETTIFY_ENDTAGS = join "", map { "</\$_>" } \@CGI::Pretty::AS_IS;
  64. if ( \$NON_PRETTIFY_ENDTAGS =~ /\$untag/ ) {
  65. \@result = map { "\$tag\$_\$untag\$CGI::Pretty::LINEBREAK" }
  66. (ref(\$_[0]) eq 'ARRAY') ? \@{\$_[0]} : "\@_";
  67. }
  68. else {
  69. \@result = map {
  70. chomp;
  71. if ( \$_ !~ /<\\// ) {
  72. s/\$CGI::Pretty::LINEBREAK/\$CGI::Pretty::LINEBREAK\$CGI::Pretty::INDENT/g if \$CGI::Pretty::LINEBREAK;
  73. }
  74. else {
  75. my \$tmp = \$_;
  76. CGI::Pretty::_prettyPrint( \\\$tmp );
  77. \$_ = \$tmp;
  78. }
  79. "\$tag\$CGI::Pretty::LINEBREAK\$CGI::Pretty::INDENT\$_\$CGI::Pretty::LINEBREAK\$untag\$CGI::Pretty::LINEBREAK" }
  80. (ref(\$_[0]) eq 'ARRAY') ? \@{\$_[0]} : "\@_";
  81. }
  82. local \$" = "";
  83. return "\@result";
  84. }
  85. };
  86. }
  87. sub start_html {
  88. return CGI::start_html( @_ ) . $CGI::Pretty::LINEBREAK;
  89. }
  90. sub end_html {
  91. return CGI::end_html( @_ ) . $CGI::Pretty::LINEBREAK;
  92. }
  93. sub new {
  94. my $class = shift;
  95. my $this = $class->SUPER::new( @_ );
  96. Apache->request->register_cleanup(\&CGI::Pretty::_reset_globals) if ($CGI::MOD_PERL);
  97. $class->_reset_globals if $CGI::PERLEX;
  98. return bless $this, $class;
  99. }
  100. sub initialize_globals {
  101. # This is the string used for indentation of tags
  102. $CGI::Pretty::INDENT = "\t";
  103. # This is the string used for seperation between tags
  104. $CGI::Pretty::LINEBREAK = "\n";
  105. # These tags are not prettify'd.
  106. @CGI::Pretty::AS_IS = qw( a pre code script textarea );
  107. 1;
  108. }
  109. sub _reset_globals { initialize_globals(); }
  110. 1;
  111. =head1 NAME
  112. CGI::Pretty - module to produce nicely formatted HTML code
  113. =head1 SYNOPSIS
  114. use CGI::Pretty qw( :html3 );
  115. # Print a table with a single data element
  116. print table( TR( td( "foo" ) ) );
  117. =head1 DESCRIPTION
  118. CGI::Pretty is a module that derives from CGI. It's sole function is to
  119. allow users of CGI to output nicely formatted HTML code.
  120. When using the CGI module, the following code:
  121. print table( TR( td( "foo" ) ) );
  122. produces the following output:
  123. <TABLE><TR><TD>foo</TD></TR></TABLE>
  124. If a user were to create a table consisting of many rows and many columns,
  125. the resultant HTML code would be quite difficult to read since it has no
  126. carriage returns or indentation.
  127. CGI::Pretty fixes this problem. What it does is add a carriage
  128. return and indentation to the HTML code so that one can easily read
  129. it.
  130. print table( TR( td( "foo" ) ) );
  131. now produces the following output:
  132. <TABLE>
  133. <TR>
  134. <TD>
  135. foo
  136. </TD>
  137. </TR>
  138. </TABLE>
  139. =head2 Tags that won't be formatted
  140. The <A> and <PRE> tags are not formatted. If these tags were formatted, the
  141. user would see the extra indentation on the web browser causing the page to
  142. look different than what would be expected. If you wish to add more tags to
  143. the list of tags that are not to be touched, push them onto the C<@AS_IS> array:
  144. push @CGI::Pretty::AS_IS,qw(CODE XMP);
  145. =head2 Customizing the Indenting
  146. If you wish to have your own personal style of indenting, you can change the
  147. C<$INDENT> variable:
  148. $CGI::Pretty::INDENT = "\t\t";
  149. would cause the indents to be two tabs.
  150. Similarly, if you wish to have more space between lines, you may change the
  151. C<$LINEBREAK> variable:
  152. $CGI::Pretty::LINEBREAK = "\n\n";
  153. would create two carriage returns between lines.
  154. If you decide you want to use the regular CGI indenting, you can easily do
  155. the following:
  156. $CGI::Pretty::INDENT = $CGI::Pretty::LINEBREAK = "";
  157. =head1 BUGS
  158. This section intentionally left blank.
  159. =head1 AUTHOR
  160. Brian Paulsen <[email protected]>, with minor modifications by
  161. Lincoln Stein <[email protected]> for incorporation into the CGI.pm
  162. distribution.
  163. Copyright 1999, Brian Paulsen. All rights reserved.
  164. This library is free software; you can redistribute it and/or modify
  165. it under the same terms as Perl itself.
  166. Bug reports and comments to Brian@ThePaulsens.com. You can also write
  167. to lstein@cshl.org, but this code looks pretty hairy to me and I'm not
  168. sure I understand it!
  169. =head1 SEE ALSO
  170. L<CGI>
  171. =cut