3 # See the bottom of this file for the POD documentation. Search for the
6 # You can run this file through either pod2man or pod2html to produce pretty
7 # documentation in manual or html file format (these utilities are part of the
8 # Perl 5 distribution).
13 $CGI::DefaultClass = __PACKAGE__;
14 $AutoloadClass = 'CGI';
17 # These tags should not be prettify'd. If we did prettify them, the
18 # browser would output text that would have extraneous spaces
20 my $NON_PRETTIFY_ENDTAGS = join "", map { "</$_>" } @AS_IS;
23 my ($self,$tagname) = @_;
24 return $self->SUPER::_make_tag_func($tagname) if $tagname=~/^(start|end)_/;
28 # handle various cases in which we're called
29 # most of this bizarre stuff is to avoid -w errors
31 # (!ref(\$_[0]) && \$_[0] eq \$CGI::DefaultClass) ||
33 (substr(ref(\$_[0]),0,3) eq 'CGI' ||
34 UNIVERSAL::isa(\$_[0],'CGI')));
37 if (ref(\$_[0]) && ref(\$_[0]) eq 'HASH') {
38 my(\@attr) = make_attributes('',shift);
39 \$attr = " \@attr" if \@attr;
42 my(\$tag,\$untag) = ("\U<$tagname\E\$attr>","\U</$tagname>\E");
43 return \$tag unless \@_;
46 if ( "$NON_PRETTIFY_ENDTAGS" =~ /\$untag/ ) {
47 \@result = map { "\$tag\$_\$untag\\n" }
48 (ref(\$_[0]) eq 'ARRAY') ? \@{\$_[0]} : "\@_";
53 if ( \$_ !~ /<\\// ) {
58 my ( \$pretag, \$thistag, \$posttag );
59 while ( /<\\/.*>/si ) {
60 if ( (\$pretag, \$thistag, \$posttag ) =
61 /(.*?)<(.*?)>(.*)/si ) {
62 \$pretag =~ s/\\n/\\n /g;
63 \$text .= "\$pretag<\$thistag>";
65 ( \$thistag ) = split ' ', \$thistag;
66 my \$endtag = "</" . uc(\$thistag) . ">";
67 if ( "$NON_PRETTIFY_ENDTAGS" =~ /\$endtag/ ) {
68 if ( ( \$pretag, \$posttag ) =
69 \$posttag =~ /(.*?)\$endtag(.*)/si ) {
70 \$text .= "\$pretag\$endtag";
78 if ( defined \$posttag ) {
79 \$posttag =~ s/\\n/\\n /g;
83 "\$tag\\n \$_\\n\$untag\\n" }
84 (ref(\$_[0]) eq 'ARRAY') ? \@{\$_[0]} : "\@_";
93 my $this = $class->SUPER::new( @_ );
95 return bless $this, $class;
102 CGI::Pretty - module to produce nicely formatted HTML code
106 use CGI::Pretty qw( :html3 );
108 # Print a table with a single data element
109 print table( TR( td( "foo" ) ) );
113 CGI::Pretty is a module that derives from CGI. It's sole function is to
114 allow users of CGI to output nicely formatted HTML code.
116 When using the CGI module, the following code:
117 print table( TR( td( "foo" ) ) );
119 produces the following output:
120 <TABLE><TR><TD>foo</TD></TR></TABLE>
122 If a user were to create a table consisting of many rows and many columns,
123 the resultant HTML code would be quite difficult to read since it has no
124 carriage returns or indentation.
126 CGI::Pretty fixes this problem. What it does is add a carriage
127 return and indentation to the HTML code so that one can easily read
130 print table( TR( td( "foo" ) ) );
132 now produces the following output:
142 =head2 Tags that won't be formatted
144 The <A> and <PRE> tags are not formatted. If these tags were formatted, the
145 user would see the extra indentation on the web browser causing the page to
146 look different than what would be expected. If you wish to add more tags to
147 the list of tags that are not to be touched, push them onto the C<@AS_IS> array:
149 push @CGI::Pretty::AS_IS,qw(CODE XMP);
153 This section intentionally left blank.
157 Brian Paulsen <bpaulsen@lehman.com>, with minor modifications by
158 Lincoln Stein <lstein@cshl.org> for incorporation into the CGI.pm
161 Copyright 1998, Brian Paulsen. All rights reserved.
163 This library is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself.
166 Bug reports and comments to bpaulsen@lehman.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!