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::Pretty::VERSION = '1.08';
14 $CGI::DefaultClass = __PACKAGE__;
15 $CGI::Pretty::AutoloadClass = 'CGI';
16 @CGI::Pretty::ISA = qw( CGI );
23 return if !$CGI::Pretty::LINEBREAK || !$CGI::Pretty::INDENT;
25 # print STDERR "'", $$input, "'\n";
27 foreach my $i ( @CGI::Pretty::AS_IS ) {
28 if ( $$input =~ m{</$i>}si ) {
29 my ( $a, $b, $c ) = $$input =~ m{(.*)(<$i[\s/>].*?</$i>)(.*)}si;
34 _prettyPrint( \$a ) if $a;
35 _prettyPrint( \$c ) if $c;
42 $$input =~ s/$CGI::Pretty::LINEBREAK/$CGI::Pretty::LINEBREAK$CGI::Pretty::INDENT/g;
46 my($self,@p) = CGI::self_or_CGI(@_);
49 $s =~ s/$CGI::Pretty::LINEBREAK/$CGI::Pretty::LINEBREAK$CGI::Pretty::INDENT/g if $CGI::Pretty::LINEBREAK;
51 return $self->SUPER::comment( "$CGI::Pretty::LINEBREAK$CGI::Pretty::INDENT$s$CGI::Pretty::LINEBREAK" ) . $CGI::Pretty::LINEBREAK;
55 my ($self,$tagname) = @_;
57 # As Lincoln as noted, the last else clause is VERY hairy, and it
58 # took me a while to figure out what I was trying to do.
59 # What it does is look for tags that shouldn't be indented (e.g. PRE)
60 # and makes sure that when we nest tags, those tags don't get
62 # For an example, try print td( pre( "hello\nworld" ) );
63 # If we didn't care about stuff like that, the code would be
64 # MUCH simpler. BTW: I won't claim to be a regular expression
65 # guru, so if anybody wants to contribute something that would
66 # be quicker, easier to read, etc, I would be more than
67 # willing to put it in - Brian
75 (substr(ref($_[0]),0,3) eq "CGI" ||
76 UNIVERSAL::isa($_[0],"CGI")));
78 if (ref($_[0]) && ref($_[0]) eq "HASH") {
79 my(@attr) = make_attributes(shift()||undef,1);
80 $attr = " @attr" if @attr;
83 if ($tagname=~/start_(\w+)/i) {
85 return "<\L$1\E\$attr>\$CGI::Pretty::LINEBREAK";} !;
86 } elsif ($tagname=~/end_(\w+)/i) {
88 return "<\L/$1\E>\$CGI::Pretty::LINEBREAK"; } !;
91 return ( \$CGI::XHTML ? "<\L$tagname\E\$attr />" : "<\L$tagname\E\$attr>" ) .
92 \$CGI::Pretty::LINEBREAK unless \@_;
93 my(\$tag,\$untag) = ("<\L$tagname\E\$attr>","</\L$tagname>\E");
95 my \%ASIS = map { lc("\$_") => 1 } \@CGI::Pretty::AS_IS;
97 if ( \$CGI::Pretty::LINEBREAK || \$CGI::Pretty::INDENT ) {
98 if(ref(\$_[0]) eq 'ARRAY') {
103 \$args[0] .= \$CGI::Pretty::LINEBREAK if \$args[0] !~ /\$CGI::Pretty::LINEBREAK\$/ && 0;
104 chomp \$args[0] if exists \$ASIS{ "\L$tagname\E" };
106 \$args[0] .= \$" if \$args[0] !~ /\$CGI::Pretty::LINEBREAK\$/ && 1;
112 \@args = ref(\$_[0]) eq 'ARRAY' ? \@{\$_[0]} : "\@_";
116 if ( exists \$ASIS{ "\L$tagname\E" } ) {
117 \@result = map { "\$tag\$_\$untag\$CGI::Pretty::LINEBREAK" }
124 CGI::Pretty::_prettyPrint( \\\$tmp );
125 \$tag . \$CGI::Pretty::LINEBREAK .
126 \$CGI::Pretty::INDENT . \$tmp . \$CGI::Pretty::LINEBREAK .
127 \$untag . \$CGI::Pretty::LINEBREAK
130 local \$" = "" if \$CGI::Pretty::LINEBREAK || \$CGI::Pretty::INDENT;
139 return CGI::start_html( @_ ) . $CGI::Pretty::LINEBREAK;
143 return CGI::end_html( @_ ) . $CGI::Pretty::LINEBREAK;
148 my $this = $class->SUPER::new( @_ );
150 if ($CGI::MOD_PERL) {
151 my $r = Apache->request;
152 if ($CGI::MOD_PERL == 1) {
153 $r->register_cleanup(\&CGI::Pretty::_reset_globals);
156 $r->pool->cleanup_register(\&CGI::Pretty::_reset_globals);
159 $class->_reset_globals if $CGI::PERLEX;
161 return bless $this, $class;
164 sub initialize_globals {
165 # This is the string used for indentation of tags
166 $CGI::Pretty::INDENT = "\t";
168 # This is the string used for seperation between tags
169 $CGI::Pretty::LINEBREAK = $/;
171 # These tags are not prettify'd.
172 @CGI::Pretty::AS_IS = qw( a pre code script textarea td );
176 sub _reset_globals { initialize_globals(); }
182 CGI::Pretty - module to produce nicely formatted HTML code
186 use CGI::Pretty qw( :html3 );
188 # Print a table with a single data element
189 print table( TR( td( "foo" ) ) );
193 CGI::Pretty is a module that derives from CGI. It's sole function is to
194 allow users of CGI to output nicely formatted HTML code.
196 When using the CGI module, the following code:
197 print table( TR( td( "foo" ) ) );
199 produces the following output:
200 <TABLE><TR><TD>foo</TD></TR></TABLE>
202 If a user were to create a table consisting of many rows and many columns,
203 the resultant HTML code would be quite difficult to read since it has no
204 carriage returns or indentation.
206 CGI::Pretty fixes this problem. What it does is add a carriage
207 return and indentation to the HTML code so that one can easily read
210 print table( TR( td( "foo" ) ) );
212 now produces the following output:
222 =head2 Tags that won't be formatted
224 The <A> and <PRE> tags are not formatted. If these tags were formatted, the
225 user would see the extra indentation on the web browser causing the page to
226 look different than what would be expected. If you wish to add more tags to
227 the list of tags that are not to be touched, push them onto the C<@AS_IS> array:
229 push @CGI::Pretty::AS_IS,qw(CODE XMP);
231 =head2 Customizing the Indenting
233 If you wish to have your own personal style of indenting, you can change the
236 $CGI::Pretty::INDENT = "\t\t";
238 would cause the indents to be two tabs.
240 Similarly, if you wish to have more space between lines, you may change the
241 C<$LINEBREAK> variable:
243 $CGI::Pretty::LINEBREAK = "\n\n";
245 would create two carriage returns between lines.
247 If you decide you want to use the regular CGI indenting, you can easily do
250 $CGI::Pretty::INDENT = $CGI::Pretty::LINEBREAK = "";
254 This section intentionally left blank.
258 Brian Paulsen <Brian@ThePaulsens.com>, with minor modifications by
259 Lincoln Stein <lstein@cshl.org> for incorporation into the CGI.pm
262 Copyright 1999, Brian Paulsen. All rights reserved.
264 This library is free software; you can redistribute it and/or modify
265 it under the same terms as Perl itself.
267 Bug reports and comments to Brian@ThePaulsens.com. You can also write
268 to lstein@cshl.org, but this code looks pretty hairy to me and I'm not
269 sure I understand it!