| blib/lib/Text/Smart/HTML.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 61 | 62 | 98.3 |
| branch | 3 | 6 | 50.0 |
| condition | 1 | 3 | 33.3 |
| subroutine | 16 | 16 | 100.0 |
| pod | 12 | 12 | 100.0 |
| total | 93 | 99 | 93.9 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | # -*- perl -*- | ||||||
| 2 | # | ||||||
| 3 | # Text::Smart::HTML by Daniel Berrange |
||||||
| 4 | # | ||||||
| 5 | # Copyright (C) 2000-2004 Daniel P. Berrange |
||||||
| 6 | # | ||||||
| 7 | # This program is free software; you can redistribute it and/or modify | ||||||
| 8 | # it under the terms of the GNU General Public License as published by | ||||||
| 9 | # the Free Software Foundation; either version 2 of the License, or | ||||||
| 10 | # (at your option) any later version. | ||||||
| 11 | # | ||||||
| 12 | # This program is distributed in the hope that it will be useful, | ||||||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
| 15 | # GNU General Public License for more details. | ||||||
| 16 | # | ||||||
| 17 | # You should have received a copy of the GNU General Public License | ||||||
| 18 | # along with this program; if not, write to the Free Software | ||||||
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||||
| 20 | # | ||||||
| 21 | # $Id: HTML.pm,v 1.2 2004/12/31 16:00:45 dan Exp $ | ||||||
| 22 | |||||||
| 23 | =pod | ||||||
| 24 | |||||||
| 25 | =head1 NAME | ||||||
| 26 | |||||||
| 27 | Text::Smart::HTML - Smart text outputter for HTML | ||||||
| 28 | |||||||
| 29 | =head1 SYNOPSIS | ||||||
| 30 | |||||||
| 31 | use Text::Smart::HTML; | ||||||
| 32 | |||||||
| 33 | my $markup = Text::Smart::HTML->new(%params); | ||||||
| 34 | |||||||
| 35 | =head1 DESCRIPTION | ||||||
| 36 | |||||||
| 37 | =head1 METHODS | ||||||
| 38 | |||||||
| 39 | =over 4 | ||||||
| 40 | |||||||
| 41 | =cut | ||||||
| 42 | |||||||
| 43 | package Text::Smart::HTML; | ||||||
| 44 | |||||||
| 45 | 1 | 1 | 38154 | use strict; | |||
| 1 | 2 | ||||||
| 1 | 34 | ||||||
| 46 | 1 | 1 | 6 | use warnings; | |||
| 1 | 4 | ||||||
| 1 | 28 | ||||||
| 47 | |||||||
| 48 | 1 | 1 | 661 | use Text::Smart; | |||
| 1 | 2 | ||||||
| 1 | 51 | ||||||
| 49 | |||||||
| 50 | 1 | 1 | 6 | use vars qw(@ISA); | |||
| 1 | 2 | ||||||
| 1 | 821 | ||||||
| 51 | |||||||
| 52 | @ISA = qw(Text::Smart); | ||||||
| 53 | |||||||
| 54 | =item my $proc = Text::Smart::HTML->new(target => $target); | ||||||
| 55 | |||||||
| 56 | Creates a new smart text processor which outputs HTML markup. | ||||||
| 57 | The only C |
||||||
| 58 | window target (via the 'target' attribute on the tag) | ||||||
| 59 | |||||||
| 60 | =cut | ||||||
| 61 | |||||||
| 62 | sub new { | ||||||
| 63 | 1 | 1 | 1 | 13 | my $proto = shift; | ||
| 64 | 1 | 33 | 10 | my $class = ref($proto) || $proto; | |||
| 65 | 1 | 12 | my $self = $class->SUPER::new(); | ||||
| 66 | 1 | 3 | my %params = @_; | ||||
| 67 | |||||||
| 68 | 1 | 50 | 10 | $self->{target} = exists $params{target} ? $params{target} : undef; | |||
| 69 | |||||||
| 70 | 1 | 3 | bless $self, $class; | ||||
| 71 | |||||||
| 72 | 1 | 5 | return $self; | ||||
| 73 | } | ||||||
| 74 | |||||||
| 75 | =item my $markup = $proc->generate_divider | ||||||
| 76 | |||||||
| 77 | Generates a horizontal divider using the tag. |
||||||
| 78 | |||||||
| 79 | =cut | ||||||
| 80 | |||||||
| 81 | sub generate_divider { | ||||||
| 82 | 1 | 1 | 1 | 2 | my $self = shift; | ||
| 83 | |||||||
| 84 | 1 | 4 | return " \n"; |
||||
| 85 | } | ||||||
| 86 | |||||||
| 87 | =item my $markup = $proc->generate_itemize(@items) | ||||||
| 88 | |||||||
| 89 | Generates an itemized list of bullet points using the
|
||||||
| 90 | |||||||
| 91 | =cut | ||||||
| 92 | |||||||
| 93 | sub generate_itemize { | ||||||
| 94 | 1 | 1 | 1 | 2 | my $self = shift; | ||
| 95 | 1 | 3 | my @items = @_; | ||||
| 96 | |||||||
| 97 | 1 | 3 | return "
|
||||
| 3 | 13 | ||||||
| 98 | } | ||||||
| 99 | |||||||
| 100 | =item my $markup = $proc->generate_enumeration(@items) | ||||||
| 101 | |||||||
| 102 | Generates an itemized list of numbered points using the
|
||||||
| 103 | |||||||
| 104 | =cut | ||||||
| 105 | |||||||
| 106 | sub generate_enumeration { | ||||||
| 107 | 1 | 1 | 1 | 2 | my $self = shift; | ||
| 108 | 1 | 3 | my @items = @_; | ||||
| 109 | |||||||
| 110 | 1 | 2 | return "
|
||||
| 2 | 12 | ||||||
| 111 | } | ||||||
| 112 | |||||||
| 113 | =item my $markup = $proc->generate_heading($text, $level) | ||||||
| 114 | |||||||
| 115 | Generates a heading using one of the tags through |
||||||
| 116 | |||||||
| 117 | =cut | ||||||
| 118 | |||||||
| 119 | sub generate_heading { | ||||||
| 120 | 6 | 6 | 1 | 10 | my $self = shift; | ||
| 121 | 6 | 7 | local $_ = $_[0]; | ||||
| 122 | 6 | 12 | my $level = $_[1]; | ||||
| 123 | |||||||
| 124 | 6 | 26 | my %levels = ( | ||||
| 125 | "title" => "h1", | ||||||
| 126 | "subtitle" => "h2", | ||||||
| 127 | "section" => "h3", | ||||||
| 128 | "subsection" => "h4", | ||||||
| 129 | "subsubsection" => "h5", | ||||||
| 130 | "paragraph" => "h6", | ||||||
| 131 | ); | ||||||
| 132 | |||||||
| 133 | 6 | 38 | return "<" . $levels{$level} . ">$_" . $levels{$level} . ">\n"; | ||||
| 134 | } | ||||||
| 135 | |||||||
| 136 | =item my $markup = $proc->generate_paragraph($text) | ||||||
| 137 | |||||||
| 138 | Gnerates a paragraph using the tag. |
||||||
| 139 | |||||||
| 140 | =cut | ||||||
| 141 | |||||||
| 142 | sub generate_paragraph { | ||||||
| 143 | 4 | 4 | 1 | 5 | my $self = shift; | ||
| 144 | 4 | 7 | local $_ = $_[0]; | ||||
| 145 | |||||||
| 146 | 4 | 22 | return " $_ \n"; |
||||
| 147 | } | ||||||
| 148 | |||||||
| 149 | =item my $markup = $proc->generate_bold($text) | ||||||
| 150 | |||||||
| 151 | Generates bold text using the tag | ||||||
| 152 | |||||||
| 153 | =cut | ||||||
| 154 | |||||||
| 155 | sub generate_bold { | ||||||
| 156 | 1 | 1 | 1 | 1 | my $self = shift; | ||
| 157 | 1 | 4 | local $_ = $_[0]; | ||||
| 158 | |||||||
| 159 | 1 | 6 | return "$_"; | ||||
| 160 | } | ||||||
| 161 | |||||||
| 162 | =item my $markup = $proc->generate_italic($text) | ||||||
| 163 | |||||||
| 164 | Generates italic text using the tag. | ||||||
| 165 | |||||||
| 166 | =cut | ||||||
| 167 | |||||||
| 168 | sub generate_italic { | ||||||
| 169 | 1 | 1 | 1 | 3 | my $self = shift; | ||
| 170 | 1 | 4 | local $_ = $_[0]; | ||||
| 171 | |||||||
| 172 | 1 | 8 | return "$_"; | ||||
| 173 | } | ||||||
| 174 | |||||||
| 175 | =item my $markup = $proc->generate_monospace($text) | ||||||
| 176 | |||||||
| 177 | Generates monospaced text using the tag. |
||||||
| 178 | |||||||
| 179 | =cut | ||||||
| 180 | |||||||
| 181 | sub generate_monospace { | ||||||
| 182 | 1 | 1 | 1 | 2 | my $self = shift; | ||
| 183 | 1 | 3 | local $_ = $_[0]; | ||||
| 184 | |||||||
| 185 | 1 | 6 | return "$_"; |
||||
| 186 | } | ||||||
| 187 | |||||||
| 188 | |||||||
| 189 | =item my $markup = $proc->generate_link($url, $text) | ||||||
| 190 | |||||||
| 191 | Generates a hyperlink using the tag. | ||||||
| 192 | |||||||
| 193 | =cut | ||||||
| 194 | |||||||
| 195 | sub generate_link { | ||||||
| 196 | 1 | 1 | 1 | 1 | my $self = shift; | ||
| 197 | 1 | 3 | my $url = shift; | ||||
| 198 | 1 | 2 | local $_ = $_[0]; | ||||
| 199 | |||||||
| 200 | 1 | 50 | 4 | if ($self->{target}) { | |||
| 201 | 0 | 0 | return "{target}\" href=\"$url\">$_"; | ||||
| 202 | } else { | ||||||
| 203 | 1 | 7 | return "$_"; | ||||
| 204 | } | ||||||
| 205 | } | ||||||
| 206 | |||||||
| 207 | |||||||
| 208 | =item my $markup = $proc->generate_entity($text) | ||||||
| 209 | |||||||
| 210 | Generates entities using the ½, ¼, ¾, | ||||||
| 211 | ©, ® and TM entities / markup. | ||||||
| 212 | |||||||
| 213 | =cut | ||||||
| 214 | |||||||
| 215 | sub generate_entity { | ||||||
| 216 | 6 | 6 | 1 | 10 | my $self = shift; | ||
| 217 | 6 | 8 | my $entity = shift; | ||||
| 218 | |||||||
| 219 | 6 | 39 | my %entities = ( | ||||
| 220 | fraction12 => "½", | ||||||
| 221 | fraction14 => "¼", | ||||||
| 222 | fraction34 => "¾", | ||||||
| 223 | copyright => "©", | ||||||
| 224 | registered => "®", | ||||||
| 225 | trademark => "TM", | ||||||
| 226 | ); | ||||||
| 227 | |||||||
| 228 | 6 | 50 | 45 | return exists $entities{$entity} ? $entities{$entity} : $entity; | |||
| 229 | } | ||||||
| 230 | |||||||
| 231 | =item my $text = $proc->escape($text) | ||||||
| 232 | |||||||
| 233 | Escapes the ampersand, and angle bracket characters | ||||||
| 234 | |||||||
| 235 | =cut | ||||||
| 236 | |||||||
| 237 | sub escape { | ||||||
| 238 | 15 | 15 | 1 | 20 | my $self = shift; | ||
| 239 | 15 | 24 | local $_ = $_[0]; | ||||
| 240 | |||||||
| 241 | 15 | 25 | s/&/&/g; | ||||
| 242 | 15 | 17 | s/</g; | ||||
| 243 | 15 | 29 | s/>/>/g; | ||||
| 244 | |||||||
| 245 | 15 | 44 | return $_; | ||||
| 246 | } | ||||||
| 247 | |||||||
| 248 | 1 # So that the require or use succeeds. | ||||||
| 249 | |||||||
| 250 | __END__ |