| 1 | package Plerd::SmartyPants; | ||||||||||||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | # Configurable variables: | ||||||||||||||||||||||||||||||||||||||
| 7 | 13 | my $smartypants_attr = "1"; | |||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | # 1 => "--" for em-dashes; no en-dash support | ||||||||||||||||||||||||||||||||||||||
| 10 | # 2 => "---" for em-dashes; "--" for en-dashes | ||||||||||||||||||||||||||||||||||||||
| 11 | # 3 => "--" for em-dashes; "---" for en-dashes | ||||||||||||||||||||||||||||||||||||||
| 12 | # See docs for more configuration options. | ||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | # Globals: | ||||||||||||||||||||||||||||||||||||||
| 15 | 13 | my $tags_to_skip = qr!<(/?)(?:pre|code|kbd|script)[\s>]!; | |||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||
| 17 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | =head1 Methods | ||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | =head2 process | ||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | Do the bulk of the conversion work. | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 26 | ▶ | sub process { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=38 SCAR=55.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 28 | 26 | ▶ | shift if ( $_[0] eq __PACKAGE__ ); # oops, called in OOP fashion. | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | # Paramaters: | ||||||||||||||||||||||||||||||||||||||
| 31 | 26 | my $text = shift; # text to be parsed | |||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | # value of the smart_quotes="" attribute. Default to 'everything on' | ||||||||||||||||||||||||||||||||||||||
| 34 | 26 | ▶ | my $attr = shift || '1'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || '1'
MC/DC0%0/1
shift() || '1'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | # Options to specify which transformations to make: | ||||||||||||||||||||||||||||||||||||||
| 37 | 26 | my ( $do_quotes, $do_backticks, $do_dashes, $do_ellipses, $do_stupefy ); | |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | # should we translate " entities into normal quotes? | ||||||||||||||||||||||||||||||||||||||
| 40 | 26 | my $convert_quot = 0; | |||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | # Parse attributes: | ||||||||||||||||||||||||||||||||||||||
| 43 | # 0 : do nothing | ||||||||||||||||||||||||||||||||||||||
| 44 | # 1 : set all | ||||||||||||||||||||||||||||||||||||||
| 45 | # 2 : set all, using old school en- and em- dash shortcuts | ||||||||||||||||||||||||||||||||||||||
| 46 | # 3 : set all, using inverted old school en and em- dash shortcuts | ||||||||||||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||||||||||||
| 48 | # q : quotes | ||||||||||||||||||||||||||||||||||||||
| 49 | # b : backtick quotes (``double'' only) | ||||||||||||||||||||||||||||||||||||||
| 50 | # B : backtick quotes (``double'' and `single') | ||||||||||||||||||||||||||||||||||||||
| 51 | # d : dashes | ||||||||||||||||||||||||||||||||||||||
| 52 | # D : old school dashes | ||||||||||||||||||||||||||||||||||||||
| 53 | # i : inverted old school dashes | ||||||||||||||||||||||||||||||||||||||
| 54 | # e : ellipses | ||||||||||||||||||||||||||||||||||||||
| 55 | # w : convert " entities to " for Dreamweaver users | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | 26 | ▶ | if ( $attr eq "0" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||
| 59 | # Do nothing. | ||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return $text; | |||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||
| 62 | elsif ( $attr eq "1" ) { | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | # Do everything, turn all options on. | ||||||||||||||||||||||||||||||||||||||
| 65 | 26 | $do_quotes = 1; | |||||||||||||||||||||||||||||||||||||
| 66 | 26 | $do_backticks = 1; | |||||||||||||||||||||||||||||||||||||
| 67 | 26 | $do_dashes = 1; | |||||||||||||||||||||||||||||||||||||
| 68 | 26 | $do_ellipses = 1; | |||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||
| 70 | elsif ( $attr eq "2" ) { | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | # Do everything, turn all options on, use old school dash shorthand. | ||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $do_quotes = 1; | |||||||||||||||||||||||||||||||||||||
| 74 | 0 | $do_backticks = 1; | |||||||||||||||||||||||||||||||||||||
| 75 | 0 | $do_dashes = 2; | |||||||||||||||||||||||||||||||||||||
| 76 | 0 | $do_ellipses = 1; | |||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||
| 78 | elsif ( $attr eq "3" ) { | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | # Do everything, turn all options on, use inverted old school dash shorthand. | ||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $do_quotes = 1; | |||||||||||||||||||||||||||||||||||||
| 82 | 0 | $do_backticks = 1; | |||||||||||||||||||||||||||||||||||||
| 83 | 0 | $do_dashes = 3; | |||||||||||||||||||||||||||||||||||||
| 84 | 0 | $do_ellipses = 1; | |||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | elsif ( $attr eq "-1" ) { | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | # Special "stupefy" mode. | ||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $do_stupefy = 1; | |||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my @chars = split( //, $attr ); | |||||||||||||||||||||||||||||||||||||
| 93 | 0 | foreach my $c (@chars) { | |||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $c eq "q" ) { $do_quotes = 1; } | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 95 | 0 | elsif ( $c eq "b" ) { $do_backticks = 1; } | |||||||||||||||||||||||||||||||||||||
| 96 | 0 | elsif ( $c eq "B" ) { $do_backticks = 2; } | |||||||||||||||||||||||||||||||||||||
| 97 | 0 | elsif ( $c eq "d" ) { $do_dashes = 1; } | |||||||||||||||||||||||||||||||||||||
| 98 | 0 | elsif ( $c eq "D" ) { $do_dashes = 2; } | |||||||||||||||||||||||||||||||||||||
| 99 | 0 | elsif ( $c eq "i" ) { $do_dashes = 3; } | |||||||||||||||||||||||||||||||||||||
| 100 | 0 | elsif ( $c eq "e" ) { $do_ellipses = 1; } | |||||||||||||||||||||||||||||||||||||
| 101 | 0 | elsif ( $c eq "w" ) { $convert_quot = 1; } | |||||||||||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||
| 104 | # Unknown attribute option, ignore. | ||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | 26 | ▶ | my $tokens ||= _tokenize($text); | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
my $tokens ||= _tokenize($text)
MC/DC0%0/2
my $tokens ||= _tokenize($text)
my $tokens
_tokenize($text)
Truth table33%1/3
A ||= B
A= my $tokensB= _tokenize($text)
| |||||||||||||||||||||||||||||||||||||||
| 110 | 26 | my $result = ''; | |||||||||||||||||||||||||||||||||||||
| 111 | 26 | my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags. | |||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | 26 | my $prev_token_last_char = ""; # This is a cheat, used to get some context | |||||||||||||||||||||||||||||||||||||
| 114 | # for one-character tokens that consist of | ||||||||||||||||||||||||||||||||||||||
| 115 | # just a quote char. What we do is remember | ||||||||||||||||||||||||||||||||||||||
| 116 | # the last character of the previous text | ||||||||||||||||||||||||||||||||||||||
| 117 | # token, to use as context to curl single- | ||||||||||||||||||||||||||||||||||||||
| 118 | # character quote tokens correctly. | ||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | 26 | foreach my $cur_token (@$tokens) { | |||||||||||||||||||||||||||||||||||||
| 121 | 166 | ▶ | if ( $cur_token->[0] eq "tag" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | # Don't mess with quotes inside tags. | ||||||||||||||||||||||||||||||||||||||
| 124 | 86 | $result .= $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 125 | 86 | ▶ | if ( $cur_token->[1] =~ m/$tags_to_skip/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 126 | 12 | ▶ | $in_pre = defined $1 && $1 eq '/' ? 0 : 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $1 and $1 eq "/"
MC/DC50%1/2
defined $1 and $1 eq "/"
defined $1
$1 eq "/"
Truth table66%2/3
A and B
A= defined $1B= $1 eq "/"
| |||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||||||||||||
| 130 | 80 | my $t = $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 131 | 80 | my $last_char = substr( $t, -1 ); # Remember last char of this token before processing. | |||||||||||||||||||||||||||||||||||||
| 132 | 80 | ▶ | if ( !$in_pre ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 133 | 76 | $t = ProcessEscapes($t); | |||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | 76 | ▶ | if ($convert_quot) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 136 | 0 | $t =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||
| 139 | 76 | ▶ | if ($do_dashes) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 140 | 76 | ▶ | $t = EducateDashes($t) if ( $do_dashes == 1 ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 141 | 76 | ▶ | $t = EducateDashesOldSchool($t) if ( $do_dashes == 2 ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 142 | 76 | ▶ | $t = EducateDashesOldSchoolInverted($t) if ( $do_dashes == 3 ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | 76 | ▶ | $t = EducateEllipses($t) if $do_ellipses; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||
| 147 | # Notes: backticks need to be processed before quotes. | ||||||||||||||||||||||||||||||||||||||
| 148 | 76 | ▶ | if ($do_backticks) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 149 | 76 | $t = EducateBackticks($t); | |||||||||||||||||||||||||||||||||||||
| 150 | 76 | ▶ | $t = EducateSingleBackticks($t) if ( $do_backticks == 2 ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||
| 153 | 76 | ▶ | if ($do_quotes) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 154 | 76 | ▶ | if ( $t eq q/'/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||
| 156 | # Special case: single-character ' token | ||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ( $prev_token_last_char =~ m/\S/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $t = "’"; | |||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $t = "‘"; | |||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||
| 164 | elsif ( $t eq q/"/ ) { | ||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||
| 166 | # Special case: single-character " token | ||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if ( $prev_token_last_char =~ m/\S/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $t = "”"; | |||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||
| 170 | else { | ||||||||||||||||||||||||||||||||||||||
| 171 | 0 | $t = "“"; | |||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||
| 174 | else { | ||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||
| 176 | # Normal case: | ||||||||||||||||||||||||||||||||||||||
| 177 | 76 | $t = EducateQuotes($t); | |||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | 76 | ▶ | $t = StupefyEntities($t) if $do_stupefy; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||
| 183 | 80 | $prev_token_last_char = $last_char; | |||||||||||||||||||||||||||||||||||||
| 184 | 80 | $result .= $t; | |||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||
| 188 | 26 | return $result; | |||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||
| 191 | =head2 SmartQuotes | ||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||
| 193 | Quotes to entities. | ||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub SmartQuotes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
SmartQuotes: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||
| 199 | # Paramaters: | ||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my $text = shift; # text to be parsed | |||||||||||||||||||||||||||||||||||||
| 201 | 0 | my $attr = shift; # value of the smart_quotes="" attribute | |||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $do_backticks; # should we educate ``backticks'' -style quotes? | |||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ( $attr == 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||
| 207 | # do nothing; | ||||||||||||||||||||||||||||||||||||||
| 208 | 0 | return $text; | |||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||
| 210 | elsif ( $attr == 2 ) { | ||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||
| 212 | # smarten ``backticks'' -style quotes | ||||||||||||||||||||||||||||||||||||||
| 213 | 0 | $do_backticks = 1; | |||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||
| 215 | else { | ||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $do_backticks = 0; | |||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||
| 219 | # Special case to handle quotes at the very end of $text when preceded by | ||||||||||||||||||||||||||||||||||||||
| 220 | # an HTML tag. Add a space to give the quote education algorithm a bit of | ||||||||||||||||||||||||||||||||||||||
| 221 | # context, so that it can guess correctly that it's a closing quote: | ||||||||||||||||||||||||||||||||||||||
| 222 | 0 | my $add_extra_space = 0; | |||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if ( $text =~ m/>['"]\z/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $add_extra_space = 1; # Remember, so we can trim the extra space later. | |||||||||||||||||||||||||||||||||||||
| 225 | 0 | $text .= " "; | |||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $tokens ||= _tokenize($text); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
my $tokens ||= _tokenize($text)
MC/DC0%0/2
my $tokens ||= _tokenize($text)
my $tokens
_tokenize($text)
Truth table0%0/3
A ||= B
A= my $tokensB= _tokenize($text)
| |||||||||||||||||||||||||||||||||||||||
| 229 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||
| 230 | 0 | my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags | |||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||
| 232 | 0 | my $prev_token_last_char = ""; # This is a cheat, used to get some context | |||||||||||||||||||||||||||||||||||||
| 233 | # for one-character tokens that consist of | ||||||||||||||||||||||||||||||||||||||
| 234 | # just a quote char. What we do is remember | ||||||||||||||||||||||||||||||||||||||
| 235 | # the last character of the previous text | ||||||||||||||||||||||||||||||||||||||
| 236 | # token, to use as context to curl single- | ||||||||||||||||||||||||||||||||||||||
| 237 | # character quote tokens correctly. | ||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||
| 239 | 0 | foreach my $cur_token (@$tokens) { | |||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | if ( $cur_token->[0] eq "tag" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||
| 242 | # Don't mess with quotes inside tags | ||||||||||||||||||||||||||||||||||||||
| 243 | 0 | $result .= $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | if ( $cur_token->[1] =~ m/$tags_to_skip/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | $in_pre = defined $1 && $1 eq '/' ? 0 : 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $1 and $1 eq "/"
MC/DC0%0/2
defined $1 and $1 eq "/"
defined $1
$1 eq "/"
Truth table0%0/3
A and B
A= defined $1B= $1 eq "/"
| |||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||
| 248 | else { | ||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $t = $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 250 | 0 | my $last_char = substr( $t, -1 ); # Remember last char of this token before processing. | |||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if ( !$in_pre ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $t = ProcessEscapes($t); | |||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | if ($do_backticks) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $t = EducateBackticks($t); | |||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ( $t eq q/'/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||
| 259 | # Special case: single-character ' token | ||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | if ( $prev_token_last_char =~ m/\S/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 261 | 0 | $t = "’"; | |||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||
| 263 | else { | ||||||||||||||||||||||||||||||||||||||
| 264 | 0 | $t = "‘"; | |||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||
| 267 | elsif ( $t eq q/"/ ) { | ||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||
| 269 | # Special case: single-character " token | ||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ( $prev_token_last_char =~ m/\S/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $t = "”"; | |||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||
| 273 | else { | ||||||||||||||||||||||||||||||||||||||
| 274 | 0 | $t = "“"; | |||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||
| 277 | else { | ||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||
| 279 | # Normal case: | ||||||||||||||||||||||||||||||||||||||
| 280 | 0 | $t = EducateQuotes($t); | |||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $prev_token_last_char = $last_char; | |||||||||||||||||||||||||||||||||||||
| 285 | 0 | $result .= $t; | |||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | if ($add_extra_space) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 290 | 0 | $result =~ s/ \z//; # Trim trailing space if we added one earlier. | |||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||
| 292 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||
| 295 | =head2 SmartDashes | ||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||
| 297 | Call the individual dash conversion to entities functions. | ||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub SmartDashes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
SmartDashes: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||
| 303 | # Paramaters: | ||||||||||||||||||||||||||||||||||||||
| 304 | 0 | my $text = shift; # text to be parsed | |||||||||||||||||||||||||||||||||||||
| 305 | 0 | my $attr = shift; # value of the smart_dashes="" attribute | |||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||
| 307 | # reference to the subroutine to use for dash education, default to EducateDashes: | ||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my $dash_sub_ref = \&EducateDashes; | |||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if ( $attr == 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||
| 312 | # do nothing; | ||||||||||||||||||||||||||||||||||||||
| 313 | 0 | return $text; | |||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||
| 315 | elsif ( $attr == 2 ) { | ||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||
| 317 | # use old smart dash shortcuts, "--" for en, "---" for em | ||||||||||||||||||||||||||||||||||||||
| 318 | 0 | $dash_sub_ref = \&EducateDashesOldSchool; | |||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||
| 320 | elsif ( $attr == 3 ) { | ||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||
| 322 | # inverse of 2, "--" for em, "---" for en | ||||||||||||||||||||||||||||||||||||||
| 323 | 0 | $dash_sub_ref = \&EducateDashesOldSchoolInverted; | |||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $tokens; | |||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | $tokens ||= _tokenize($text); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$tokens ||= _tokenize($text)
MC/DC0%0/2
$tokens ||= _tokenize($text)
$tokens
_tokenize($text)
Truth table0%0/3
A ||= B
A= $tokensB= _tokenize($text)
| |||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||
| 329 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||
| 330 | 0 | my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags | |||||||||||||||||||||||||||||||||||||
| 331 | 0 | foreach my $cur_token (@$tokens) { | |||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | if ( $cur_token->[0] eq "tag" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||
| 334 | # Don't mess with quotes inside tags | ||||||||||||||||||||||||||||||||||||||
| 335 | 0 | $result .= $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if ( $cur_token->[1] =~ m/$tags_to_skip/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | $in_pre = defined $1 && $1 eq '/' ? 0 : 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $1 and $1 eq "/"
MC/DC0%0/2
defined $1 and $1 eq "/"
defined $1
$1 eq "/"
Truth table0%0/3
A and B
A= defined $1B= $1 eq "/"
| |||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||
| 340 | else { | ||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my $t = $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | if ( !$in_pre ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 343 | 0 | $t = ProcessEscapes($t); | |||||||||||||||||||||||||||||||||||||
| 344 | 0 | $t = $dash_sub_ref->($t); | |||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||
| 346 | 0 | $result .= $t; | |||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||
| 349 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||
| 352 | =head2 SmartEllipses | ||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||
| 354 | Call the individual ellipse conversion to entities functions. | ||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||
| 356 | =cut | ||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | sub SmartEllipses { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
SmartEllipses: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||
| 360 | # Paramaters: | ||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my $text = shift; # text to be parsed | |||||||||||||||||||||||||||||||||||||
| 362 | 0 | my $attr = shift; # value of the smart_ellipses="" attribute | |||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | if ( $attr == 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||
| 366 | # do nothing; | ||||||||||||||||||||||||||||||||||||||
| 367 | 0 | return $text; | |||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||
| 370 | 0 | my $tokens; | |||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | $tokens ||= _tokenize($text); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$tokens ||= _tokenize($text)
MC/DC0%0/2
$tokens ||= _tokenize($text)
$tokens
_tokenize($text)
Truth table0%0/3
A ||= B
A= $tokensB= _tokenize($text)
| |||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||
| 373 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||
| 374 | 0 | my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags | |||||||||||||||||||||||||||||||||||||
| 375 | 0 | foreach my $cur_token (@$tokens) { | |||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | if ( $cur_token->[0] eq "tag" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||
| 378 | # Don't mess with quotes inside tags | ||||||||||||||||||||||||||||||||||||||
| 379 | 0 | $result .= $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | if ( $cur_token->[1] =~ m/$tags_to_skip/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | $in_pre = defined $1 && $1 eq '/' ? 0 : 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $1 and $1 eq "/"
MC/DC0%0/2
defined $1 and $1 eq "/"
defined $1
$1 eq "/"
Truth table0%0/3
A and B
A= defined $1B= $1 eq "/"
| |||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||
| 384 | else { | ||||||||||||||||||||||||||||||||||||||
| 385 | 0 | my $t = $cur_token->[1]; | |||||||||||||||||||||||||||||||||||||
| 386 | 0 | ▶ | if ( !$in_pre ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 387 | 0 | $t = ProcessEscapes($t); | |||||||||||||||||||||||||||||||||||||
| 388 | 0 | $t = EducateEllipses($t); | |||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||
| 390 | 0 | $result .= $t; | |||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||
| 393 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||
| 396 | =head2 EducateQuotes | ||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||
| 398 | Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||
| 400 | Returns: The string, with "educated" curly quote characters. | ||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||
| 402 | Example input: "Isn't this fun?" | ||||||||||||||||||||||||||||||||||||||
| 403 | Example output: “Isn’t this fun?” | ||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||
| 405 | =cut | ||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||
| 407 | 76 | ▶ | sub EducateQuotes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
EducateQuotes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 408 | 76 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||
| 410 | # Tell perl not to gripe when we use $1 in substitutions, | ||||||||||||||||||||||||||||||||||||||
| 411 | # even when it's undefined. Use $^W instead of "no warnings" | ||||||||||||||||||||||||||||||||||||||
| 412 | # for compatibility with Perl 5.005: | ||||||||||||||||||||||||||||||||||||||
| 413 | 76 | local $^W = 0; | |||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||
| 415 | # Make our own "punctuation" character class, because the POSIX-style | ||||||||||||||||||||||||||||||||||||||
| 416 | # [:PUNCT:] is only available in Perl 5.6 or later: | ||||||||||||||||||||||||||||||||||||||
| 417 | 76 | my $punct_class = qr/[!"#\$\%'()*+,-.\/:;<=>?\@\[\\\]\^_`{|}~]/; | |||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||
| 419 | # Special case if the very first character is a quote | ||||||||||||||||||||||||||||||||||||||
| 420 | # followed by punctuation at a non-word-break. Close the quotes by brute force: | ||||||||||||||||||||||||||||||||||||||
| 421 | 76 | s/^'(?=$punct_class\B)/’/; | |||||||||||||||||||||||||||||||||||||
| 422 | 76 | s/^"(?=$punct_class\B)/”/; | |||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||
| 424 | # Special case for double sets of quotes, e.g.: | ||||||||||||||||||||||||||||||||||||||
| 425 | # <p>He said, "'Quoted' words in a larger quote."</p> | ||||||||||||||||||||||||||||||||||||||
| 426 | 76 | s/"'(?=\w)/“‘/g; | |||||||||||||||||||||||||||||||||||||
| 427 | 76 | s/'"(?=\w)/‘“/g; | |||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||
| 429 | 76 | my $close_class = qr![^\ \t\r\n\[\{\(]!; | |||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||
| 431 | # XXX Shutting off warnings here, lest $1 often being undef in the | ||||||||||||||||||||||||||||||||||||||
| 432 | # substitution below trips it. The best thing to do would involve | ||||||||||||||||||||||||||||||||||||||
| 433 | # substituting with e.g. {$1 // q[] . q[']}e, but that would require | ||||||||||||||||||||||||||||||||||||||
| 434 | # Perl 5.10 or above and I'm not prepared to require that just yet. | ||||||||||||||||||||||||||||||||||||||
| 435 | 13 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||
| 437 | # Single closing quotes: | ||||||||||||||||||||||||||||||||||||||
| 438 | 76 | s { | |||||||||||||||||||||||||||||||||||||
| 439 | ($close_class)? | ||||||||||||||||||||||||||||||||||||||
| 440 | ' | ||||||||||||||||||||||||||||||||||||||
| 441 | (?(1)| # If $1 captured, then do nothing; | ||||||||||||||||||||||||||||||||||||||
| 442 | (?=\s | s\b) # otherwise, positive lookahead for a whitespace | ||||||||||||||||||||||||||||||||||||||
| 443 | ) # char or an 's' at a word ending position. This | ||||||||||||||||||||||||||||||||||||||
| 444 | # is a special case to handle something like: | ||||||||||||||||||||||||||||||||||||||
| 445 | # "<i>Custer</i>'s Last Stand." | ||||||||||||||||||||||||||||||||||||||
| 446 | } {$1’}xgi; | ||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||
| 448 | # Single opening quotes: | ||||||||||||||||||||||||||||||||||||||
| 449 | 76 | s/'/‘/g; | |||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||
| 451 | # Double closing quotes: | ||||||||||||||||||||||||||||||||||||||
| 452 | 76 | s { | |||||||||||||||||||||||||||||||||||||
| 453 | ($close_class)? | ||||||||||||||||||||||||||||||||||||||
| 454 | " | ||||||||||||||||||||||||||||||||||||||
| 455 | (?(1)|(?=\s)) # If $1 captured, then do nothing; | ||||||||||||||||||||||||||||||||||||||
| 456 | # if not, then make sure the next char is whitespace. | ||||||||||||||||||||||||||||||||||||||
| 457 | } {$1”}xg; | ||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||
| 459 | # Double opening quotes: | ||||||||||||||||||||||||||||||||||||||
| 460 | 76 | s/"/“/g; | |||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||
| 462 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||
| 464 | 76 | return $_; | |||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||
| 467 | =head2 EducateBackticks | ||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||
| 469 | Replace double (back)ticks w/ characters. | ||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||
| 471 | =cut | ||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||
| 473 | 76 | ▶ | sub EducateBackticks { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
EducateBackticks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||
| 475 | # | ||||||||||||||||||||||||||||||||||||||
| 476 | # Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 477 | # Returns: The string, with ``backticks'' -style double quotes | ||||||||||||||||||||||||||||||||||||||
| 478 | # translated into HTML curly quote entities. | ||||||||||||||||||||||||||||||||||||||
| 479 | # | ||||||||||||||||||||||||||||||||||||||
| 480 | # Example input: ``Isn't this fun?'' | ||||||||||||||||||||||||||||||||||||||
| 481 | # Example output: “Isn't this fun?” | ||||||||||||||||||||||||||||||||||||||
| 482 | # | ||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||
| 484 | 76 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 485 | 76 | s/``/“/g; | |||||||||||||||||||||||||||||||||||||
| 486 | 76 | s/''/”/g; | |||||||||||||||||||||||||||||||||||||
| 487 | 76 | return $_; | |||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||
| 490 | =head2 EducateSingleBackticks | ||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||
| 492 | Replace single (back)ticks w/ characters. | ||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||
| 494 | =cut | ||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||
| 496 | 0 | ▶ | sub EducateSingleBackticks { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
EducateSingleBackticks: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||
| 498 | # | ||||||||||||||||||||||||||||||||||||||
| 499 | # Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 500 | # Returns: The string, with `backticks' -style single quotes | ||||||||||||||||||||||||||||||||||||||
| 501 | # translated into HTML curly quote entities. | ||||||||||||||||||||||||||||||||||||||
| 502 | # | ||||||||||||||||||||||||||||||||||||||
| 503 | # Example input: `Isn't this fun?' | ||||||||||||||||||||||||||||||||||||||
| 504 | # Example output: ‘Isn’t this fun?’ | ||||||||||||||||||||||||||||||||||||||
| 505 | # | ||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||
| 507 | 0 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 508 | 0 | s/`/‘/g; | |||||||||||||||||||||||||||||||||||||
| 509 | 0 | s/'/’/g; | |||||||||||||||||||||||||||||||||||||
| 510 | 0 | return $_; | |||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||
| 513 | =head2 EducateDashes | ||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||
| 515 | Dashes to character | ||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||
| 517 | Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||
| 519 | Returns: The string, with each instance of "--" translated to | ||||||||||||||||||||||||||||||||||||||
| 520 | an em-dash character. | ||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||
| 522 | =cut | ||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||
| 524 | 76 | ▶ | sub EducateDashes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
EducateDashes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||
| 526 | 76 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 527 | 76 | s/--/—/g; | |||||||||||||||||||||||||||||||||||||
| 528 | 76 | return $_; | |||||||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||
| 531 | =head2 EducateDashesOldSchool | ||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||
| 533 | Dashes to entities. | ||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||
| 536 | Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||
| 538 | Returns: The string, with each instance of "--" translated to | ||||||||||||||||||||||||||||||||||||||
| 539 | an en-dash character, and each "---" translated to | ||||||||||||||||||||||||||||||||||||||
| 540 | an em-dash character. | ||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||
| 543 | =cut | ||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||
| 545 | 0 | ▶ | sub EducateDashesOldSchool { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
EducateDashesOldSchool: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||
| 547 | 0 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 548 | 0 | s/---/—/g; # em | |||||||||||||||||||||||||||||||||||||
| 549 | 0 | s/--/–/g; # en | |||||||||||||||||||||||||||||||||||||
| 550 | 0 | return $_; | |||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||
| 553 | =head2 EducateDashesOldSchoolInverted | ||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||
| 556 | Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||
| 558 | Returns: The string, with each instance of "--" translated to | ||||||||||||||||||||||||||||||||||||||
| 559 | an em-dash character, and each "---" translated to | ||||||||||||||||||||||||||||||||||||||
| 560 | an en-dash character. Two reasons why: First, unlike the | ||||||||||||||||||||||||||||||||||||||
| 561 | en- and em-dash syntax supported by | ||||||||||||||||||||||||||||||||||||||
| 562 | EducateDashesOldSchool(), it's compatible with existing | ||||||||||||||||||||||||||||||||||||||
| 563 | entries written before SmartyPants 1.1, back when "--" was | ||||||||||||||||||||||||||||||||||||||
| 564 | only used for em-dashes. Second, em-dashes are more | ||||||||||||||||||||||||||||||||||||||
| 565 | common than en-dashes, and so it sort of makes sense that | ||||||||||||||||||||||||||||||||||||||
| 566 | the shortcut should be shorter to type. (Thanks to Aaron | ||||||||||||||||||||||||||||||||||||||
| 567 | Swartz for the idea.) | ||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||
| 570 | =cut | ||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||
| 572 | 0 | ▶ | sub EducateDashesOldSchoolInverted { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
EducateDashesOldSchoolInverted: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||
| 574 | 0 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 575 | 0 | s/---/–/g; # en | |||||||||||||||||||||||||||||||||||||
| 576 | 0 | s/--/—/g; # em | |||||||||||||||||||||||||||||||||||||
| 577 | 0 | return $_; | |||||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||
| 580 | =head2 EducateEllipses | ||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||
| 582 | Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 583 | Returns: The string, with each instance of "..." translated to | ||||||||||||||||||||||||||||||||||||||
| 584 | an ellipsis character. | ||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||
| 586 | Example input: Huh...? | ||||||||||||||||||||||||||||||||||||||
| 587 | Example output: Huh…? | ||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||
| 589 | =cut | ||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||
| 591 | 76 | ▶ | sub EducateEllipses { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
EducateEllipses: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||
| 593 | 76 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 594 | 76 | s/\.\.\./…/g; | |||||||||||||||||||||||||||||||||||||
| 595 | 76 | return $_; | |||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||
| 598 | =head2 ProcessEscapes | ||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||
| 600 | Parameter: String. | ||||||||||||||||||||||||||||||||||||||
| 601 | Returns: The string, with after processing the following backslash | ||||||||||||||||||||||||||||||||||||||
| 602 | escape sequences. This is useful if you want to force a "dumb" | ||||||||||||||||||||||||||||||||||||||
| 603 | quote or other character to appear. | ||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||
| 605 | Escape Value | ||||||||||||||||||||||||||||||||||||||
| 606 | ------ ----- | ||||||||||||||||||||||||||||||||||||||
| 607 | \\ \ | ||||||||||||||||||||||||||||||||||||||
| 608 | \" " | ||||||||||||||||||||||||||||||||||||||
| 609 | \' ' | ||||||||||||||||||||||||||||||||||||||
| 610 | \. . | ||||||||||||||||||||||||||||||||||||||
| 611 | \- - | ||||||||||||||||||||||||||||||||||||||
| 612 | \` ` | ||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||
| 614 | =cut | ||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||
| 616 | 76 | ▶ | sub ProcessEscapes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
ProcessEscapes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||
| 618 | 76 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||
| 620 | 76 | s! \\\\ !\\!gx; | |||||||||||||||||||||||||||||||||||||
| 621 | 76 | s! \\" !"!gx; | |||||||||||||||||||||||||||||||||||||
| 622 | 76 | s! \\' !'!gx; | |||||||||||||||||||||||||||||||||||||
| 623 | 76 | s! \\\. !.!gx; | |||||||||||||||||||||||||||||||||||||
| 624 | 76 | s! \\- !-!gx; | |||||||||||||||||||||||||||||||||||||
| 625 | 76 | s! \\` !`!gx; | |||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||
| 627 | 76 | return $_; | |||||||||||||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||
| 630 | 26 | ▶ | sub _tokenize { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||
| 632 | # | ||||||||||||||||||||||||||||||||||||||
| 633 | # Parameter: String containing HTML markup. | ||||||||||||||||||||||||||||||||||||||
| 634 | # Returns: Reference to an array of the tokens comprising the input | ||||||||||||||||||||||||||||||||||||||
| 635 | # string. Each token is either a tag (possibly with nested, | ||||||||||||||||||||||||||||||||||||||
| 636 | # tags contained therein, such as <a href="<MTFoo>">, or a | ||||||||||||||||||||||||||||||||||||||
| 637 | # run of text between tags. Each element of the array is a | ||||||||||||||||||||||||||||||||||||||
| 638 | # two-element array; the first is either 'tag' or 'text'; | ||||||||||||||||||||||||||||||||||||||
| 639 | # the second is the actual value. | ||||||||||||||||||||||||||||||||||||||
| 640 | # | ||||||||||||||||||||||||||||||||||||||
| 641 | # | ||||||||||||||||||||||||||||||||||||||
| 642 | # Based on the _tokenize() subroutine from Brad Choate's MTRegex plugin. | ||||||||||||||||||||||||||||||||||||||
| 643 | # <http://www.bradchoate.com/past/mtregex.php> | ||||||||||||||||||||||||||||||||||||||
| 644 | # | ||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||
| 646 | 26 | my $str = shift; | |||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||
| 648 | 26 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||
| 649 | 26 | my $len = length $str; | |||||||||||||||||||||||||||||||||||||
| 650 | 26 | my @tokens; | |||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||
| 652 | # pattern to match balanced nested <> pairs, up to two levels deep: | ||||||||||||||||||||||||||||||||||||||
| 653 | 26 | my $nested_angles = qr/<(?:[^<>]|<[^<>]*>)*>/; | |||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||
| 655 | 26 | ▶ | while ( $str =~ m/($nested_angles)/gs ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 656 | 86 | my $whole_tag = $1; | |||||||||||||||||||||||||||||||||||||
| 657 | 86 | my $sec_start = pos $str; | |||||||||||||||||||||||||||||||||||||
| 658 | 86 | my $tag_start = $sec_start - length $whole_tag; | |||||||||||||||||||||||||||||||||||||
| 659 | 86 | ▶ | if ( $pos < $tag_start ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 660 | 54 | push @tokens, [ 'text', substr( $str, $pos, $tag_start - $pos ) ]; | |||||||||||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||||||||||||
| 662 | 86 | push @tokens, [ 'tag', $whole_tag ]; | |||||||||||||||||||||||||||||||||||||
| 663 | 86 | $pos = pos $str; | |||||||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||||||
| 665 | 26 | ▶ | push @tokens, [ 'text', substr( $str, $pos, $len - $pos ) ] if $pos < $len; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 666 | 26 | \@tokens; | |||||||||||||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||
| 669 | 13 | 1; | |||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||
| 671 | =pod | ||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||
| 673 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||
| 675 | Plerd::SmartyPants | ||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||
| 677 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||
| 679 | This is a fork of Text::SmartyPants version 1.3, originally by John Gruber. (See | ||||||||||||||||||||||||||||||||||||||
| 680 | L<"Copyright and License">, below.) It has been simplified and customized for | ||||||||||||||||||||||||||||||||||||||
| 681 | use by the Plerd static blog generator by Jason McIntosh. | ||||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||
| 683 | Unlike Text::SmartyPants, it's not intended for use in any other context. | ||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||
| 685 | The biggest difference between Plerd::SmartyPants and Text::SmartyPants is that | ||||||||||||||||||||||||||||||||||||||
| 686 | the former doesn't convert anything into HTML entities; instead it converts | ||||||||||||||||||||||||||||||||||||||
| 687 | its ASCII input directly into UTF-8 characters. | ||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||
| 689 | =head1 Copyright and License | ||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||
| 691 | Copyright (c) 2003 John Gruber | ||||||||||||||||||||||||||||||||||||||
| 692 | (http://daringfireball.net/) | ||||||||||||||||||||||||||||||||||||||
| 693 | All rights reserved. | ||||||||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||||||||
| 695 | Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||||||||||||||||||||
| 696 | modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||
| 698 | * Redistributions of source code must retain the above copyright | ||||||||||||||||||||||||||||||||||||||
| 699 | notice, this list of conditions and the following disclaimer. | ||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||
| 701 | * Redistributions in binary form must reproduce the above copyright | ||||||||||||||||||||||||||||||||||||||
| 702 | notice, this list of conditions and the following disclaimer in the | ||||||||||||||||||||||||||||||||||||||
| 703 | documentation and/or other materials provided with the distribution. | ||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||
| 705 | * Neither the name "SmartyPants" nor the names of its contributors may | ||||||||||||||||||||||||||||||||||||||
| 706 | be used to endorse or promote products derived from this software | ||||||||||||||||||||||||||||||||||||||
| 707 | without specific prior written permission. | ||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||
| 709 | This software is provided by the copyright holders and contributors "as is" | ||||||||||||||||||||||||||||||||||||||
| 710 | and any express or implied warranties, including, but not limited to, the | ||||||||||||||||||||||||||||||||||||||
| 711 | implied warranties of merchantability and fitness for a particular purpose | ||||||||||||||||||||||||||||||||||||||
| 712 | are disclaimed. In no event shall the copyright owner or contributors be | ||||||||||||||||||||||||||||||||||||||
| 713 | liable for any direct, indirect, incidental, special, exemplary, or | ||||||||||||||||||||||||||||||||||||||
| 714 | consequential damages (including, but not limited to, procurement of | ||||||||||||||||||||||||||||||||||||||
| 715 | substitute goods or services; loss of use, data, or profits; or business | ||||||||||||||||||||||||||||||||||||||
| 716 | interruption) however caused and on any theory of liability, whether in | ||||||||||||||||||||||||||||||||||||||
| 717 | contract, strict liability, or tort (including negligence or otherwise) | ||||||||||||||||||||||||||||||||||||||
| 718 | arising in any way out of the use of this software, even if advised of the | ||||||||||||||||||||||||||||||||||||||
| 719 | possibility of such damage. | ||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||
| 721 | =cut | ||||||||||||||||||||||||||||||||||||||