| 1 | # | ||
| 2 | # $Id: HTML.pm,v 0.1 2001/04/22 17:57:03 ram Exp $ | ||
| 3 | # | ||
| 4 | # Copyright (c) 1998-2001, Raphael Manfredi | ||
| 5 | # Copyright (c) 2000-2001, Christophe Dehaudt | ||
| 6 | # | ||
| 7 | # You may redistribute only under the terms of the Artistic License, | ||
| 8 | # as specified in the README file that comes with the distribution. | ||
| 9 | # | ||
| 10 | # HISTORY | ||
| 11 | # $Log: HTML.pm,v $ | ||
| 12 | # Revision 0.1 2001/04/22 17:57:03 ram | ||
| 13 | # Baseline for first Alpha release. | ||
| 14 | # | ||
| 15 | # $EndLog$ | ||
| 16 | # | ||
| 17 | |||
| 18 | # | ||
| 19 | # HTML generation utils for the CGI::MxScreen framework. | ||
| 20 | # | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | |||
| 24 | package CGI::MxScreen::HTML; | ||
| 25 | |||
| 26 | use vars qw(@EXPORT @EXPORT_OK @ISA); | ||
| 27 | |||
| 28 | require Exporter; | ||
| 29 | @ISA = qw(Exporter); | ||
| 30 | |||
| 31 | use Getargs::Long; | ||
| 32 | use CGI qw/:html/; | ||
| 33 | |||
| 34 | @EXPORT = qw( | ||
| 35 | color red yellow orange green blue purple violet magenta cyan | ||
| 36 | flash blink center | ||
| 37 | escape_HTML | ||
| 38 | unescape_HTML | ||
| 39 | ); | ||
| 40 | |||
| 41 | # | ||
| 42 | # Color makers and useful wrappers | ||
| 43 | # | ||
| 44 | |||
| 45 | BEGIN { | ||
| 46 | no strict 'refs'; | ||
| 47 | my @rainbow = qw[red yellow orange green blue purple violet]; | ||
| 48 | my @others = qw[magenta cyan]; | ||
| 49 | for my $color (@rainbow, @others) { | ||
| 50 | *$color = sub { qq<<FONT COLOR="\U$color\E">@_</FONT>> }; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub color { | ||
| 55 | my $shade = shift; | ||
| 56 | qq<<FONT COLOR="\U$shade\E">@_</FONT>>; | ||
| 57 | }; | ||
| 58 | |||
| 59 | sub flash { qq[<FLASH>@_</FLASH>] } | ||
| 60 | sub blink { qq[<BLINK>@_</BLINK>] } | ||
| 61 | sub center { qq[<CENTER>@_</CENTER>] } | ||
| 62 | |||
| 63 | # | ||
| 64 | # escape_HTML | ||
| 65 | # | ||
| 66 | # Escape the HTML special characters, for safe printing in <PRE> sections | ||
| 67 | # for instance. | ||
| 68 | # | ||
| 69 | sub escape_HTML { | ||
| 70 | my ($t) = @_; | ||
| 71 | $t =~ s/&/&/g; # Must come first | ||
| 72 | $t =~ s/\"/"/g; | ||
| 73 | $t =~ s/>/>/g; | ||
| 74 | $t =~ s/</</g; | ||
| 75 | return $t; | ||
| 76 | } | ||
| 77 | |||
| 78 | # | ||
| 79 | # unescape_HTML | ||
| 80 | # | ||
| 81 | # Un-escape all HTML escaped sequences. | ||
| 82 | # | ||
| 83 | sub unescape_HTML { | ||
| 84 | my ($s) = @_; | ||
| 85 | $s =~ s/"/\"/ig; | ||
| 86 | $s =~ s/>/>/ig; | ||
| 87 | $s =~ s/</</ig; | ||
| 88 | $s =~ s/&/&/ig; # Must come last | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | =head1 NAME | ||
| 94 | |||
| 95 | CGI::MxScreen::HTML - various HTML utility routines | ||
| 96 | |||
| 97 | =head1 SYNOPSIS | ||
| 98 | |||
| 99 | use CGI::MxScreen::HTML; | ||
| 100 | |||
| 101 | # Colours | ||
| 102 | print p("Those are ".red("red words")); | ||
| 103 | |||
| 104 | # Extra HTML tags | ||
| 105 | print center( | ||
| 106 | p( | ||
| 107 | flash("flashing") . " and " . blink("blinking") . " centered words" | ||
| 108 | ) | ||
| 109 | ); | ||
| 110 | |||
| 111 | # HTML escapes | ||
| 112 | my $escaped = escape_HTML("This & that <will> show"); | ||
| 113 | print "<p>$escaped</p>"; | ||
| 114 | my $str = unescape_HTML($escaped); | ||
| 115 | |||
| 116 | =head1 DESCRIPTION | ||
| 117 | |||
| 118 | This package holds various utility routines taken out of Tom Christiansen's | ||
| 119 | C<MxScreen> program (a "graphical" front-end to his I<Magic: The Gathering> | ||
| 120 | database) which greatly inspired this framework. | ||
| 121 | |||
| 122 | =head2 Colours | ||
| 123 | |||
| 124 | Those routines simply emit text within enclosing HTML tags. The following | ||
| 125 | color routines are defined: | ||
| 126 | |||
| 127 | red yellow orange green blue purple violet magenta cyan | ||
| 128 | |||
| 129 | For instance: | ||
| 130 | |||
| 131 | print p(big(strong(red("WARNING:")))); | ||
| 132 | |||
| 133 | would print a big boldface (usually) WARNING: in red. | ||
| 134 | |||
| 135 | =head2 Non-portable HTML tags | ||
| 136 | |||
| 137 | The following routine add non-portable HTML tags that were introduced by | ||
| 138 | Netscape. I don't recommend their use, but if you can't avoid it, they are: | ||
| 139 | |||
| 140 | flash blink center | ||
| 141 | |||
| 142 | For instance: | ||
| 143 | |||
| 144 | print center(h1("Title")); | ||
| 145 | |||
| 146 | would print a level-1 header centered. | ||
| 147 | |||
| 148 | =head2 HTML escaping | ||
| 149 | |||
| 150 | Two routines, C<escape_HTML()> and C<unescape_HTML()> perform basic HTML | ||
| 151 | quoting and un-quoting. By I<basic>, I mean they only take care of | ||
| 152 | escaping (and repectively unescaping) the "&", "<" and ">" characters. The | ||
| 153 | quote character (") is also escaped as """. | ||
| 154 | |||
| 155 | =head1 AUTHORS | ||
| 156 | |||
| 157 | Tom Christiansen F<E<lt>tchrist@perl.comE<gt>> within his MxScreen program. | ||
| 158 | |||
| 159 | Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>> for the repackaging | ||
| 160 | within the C<CGI::MxScreen> framework. | ||
| 161 | |||
| 162 | Send bug reports, suggestions, problems or questions to | ||
| 163 | Jason Purdy F<E<lt>Jason@Purdy.INFOE<gt>> | ||
| 164 | |||
| 165 | |||
| 166 | =head1 SEE ALSO | ||
| 167 | |||
| 168 | CGI::MxScreen(3). | ||
| 169 | |||
| 170 | =cut | ||
| 171 |