| 1 | package MKDoc::Text::Structured::Inline; | ||||||||||||
| 2 | 21 | ▶ | use URI::Find; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 21 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 21 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 21 | our $Text = ''; | |||||||||||
| 7 | |||||||||||||
| 8 | 21 | our $LongestWord = 78; | |||||||||||
| 9 | 21 | our $NoFollow = 0; | |||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | MKDoc::Text::Structured::Inline - convert text to HTML without handling block-level tags | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | my $text = some_structured_text(); | ||||||||||||
| 18 | my $this = MKDoc::Text::Structured::Inline::process ($text); | ||||||||||||
| 19 | my $that = MKDoc::Text::Structured::Inline::process_entities_only ($text); | ||||||||||||
| 20 | |||||||||||||
| 21 | =head1 SUMMARY | ||||||||||||
| 22 | |||||||||||||
| 23 | L<MKDoc::Text::Structured::Inline> is used by L<MKDoc::Text::Structured> to | ||||||||||||
| 24 | generate inline HTML elements such as hyperlinks, emphasis and entities. | ||||||||||||
| 25 | |||||||||||||
| 26 | This module is also useful directly when the full block-level rendering of | ||||||||||||
| 27 | L<MKDoc::Text::Structured> is unwanted. | ||||||||||||
| 28 | |||||||||||||
| 29 | =head1 USAGE | ||||||||||||
| 30 | |||||||||||||
| 31 | =head2 Processing text and adding HTML tags | ||||||||||||
| 32 | |||||||||||||
| 33 | For example, when processing text that is going to end up in an <h1> header, | ||||||||||||
| 34 | you wouldn't want any block level tags generated: | ||||||||||||
| 35 | |||||||||||||
| 36 | $header = "< My (c) symbol should be *bold* > -- and http://example.com/ 'linked'"; | ||||||||||||
| 37 | $header = MKDoc::Text::Structured::Inline::process ($title); | ||||||||||||
| 38 | |||||||||||||
| 39 | $header is now: | ||||||||||||
| 40 | |||||||||||||
| 41 | < My © symbol should be <strong>bold</strong> > — and <a href="http://example.com/">http://example.com/<a> ‘linked’ | ||||||||||||
| 42 | |||||||||||||
| 43 | =cut | ||||||||||||
| 44 | |||||||||||||
| 45 | sub process | ||||||||||||
| 46 | 93 | ▶ | { | ||||||||||
|
Subroutine
process: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 47 | 93 | local $Text; | |||||||||||
| 48 | 93 | $Text = shift; | |||||||||||
| 49 | 93 | $Text = " $Text "; | |||||||||||
| 50 | 93 | $Text =~ s/\n/ /gsm; | |||||||||||
| 51 | |||||||||||||
| 52 | 93 | _make_entities(); | |||||||||||
| 53 | |||||||||||||
| 54 | 93 | $Text =~ s/>/ >/g; | |||||||||||
| 55 | # automagically finds hyperlinks | ||||||||||||
| 56 | my $finder = URI::Find->new ( | ||||||||||||
| 57 | 10 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 58 | 10 | my ($uri, $orig_uri) = @_; | |||||||||||
| 59 | 10 | $orig_uri =~ s/^mailto://; | |||||||||||
| 60 | |||||||||||||
| 61 | # http://googleblog.blogspot.com/2005/01/preventing-comment-spam.html | ||||||||||||
| 62 | 10 | ▶ | if ($NoFollow) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | { | ||||||||||||
| 64 | 3 | return qq|<a href="$uri" rel="nofollow">$orig_uri</a>|; | |||||||||||
| 65 | } | ||||||||||||
| 66 | else | ||||||||||||
| 67 | { | ||||||||||||
| 68 | 7 | return qq|<a href="$uri">$orig_uri</a>|; | |||||||||||
| 69 | } | ||||||||||||
| 70 | } | ||||||||||||
| 71 | 93 | ); | |||||||||||
| 72 | 93 | $finder->find (\$Text); | |||||||||||
| 73 | 93 | $Text =~ s/ >/>/g; | |||||||||||
| 74 | |||||||||||||
| 75 | # abbreviations | ||||||||||||
| 76 | 93 | ▶ | while ($Text =~ s/([[:upper:]][[:upper:]]+)\s+(\(.*?\))/_make_abbr_implicit ($1, $2)/e) {}; # implicit | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 93 | ▶ | while ($Text =~ s/([[:upper:]][[:upper:]]+)(\(.*?\))/_make_abbr_explicit ($1, $2)/e) {}; # explicit | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 93 | _make_simplequotes(); | |||||||||||
| 79 | 93 | _make_doublequotes(); | |||||||||||
| 80 | 93 | _make_strong(); | |||||||||||
| 81 | 93 | _make_em(); | |||||||||||
| 82 | 93 | _make_smilies(); | |||||||||||
| 83 | 93 | _break_long_words(); | |||||||||||
| 84 | |||||||||||||
| 85 | 93 | $Text =~ s/^ //; | |||||||||||
| 86 | 93 | $Text =~ s/ $//; | |||||||||||
| 87 | 93 | return $Text; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | =pod | ||||||||||||
| 91 | |||||||||||||
| 92 | =head2 Processing text without adding tags | ||||||||||||
| 93 | |||||||||||||
| 94 | Another example, if you were processing text that will end up in an HTML | ||||||||||||
| 95 | <title> tag, this tag should never contain any other tags, so you should use | ||||||||||||
| 96 | the MKDoc::Text::Structured::Inline::process_entities_only() method: | ||||||||||||
| 97 | |||||||||||||
| 98 | $title = "< My (c) symbol shouldn't be *bold* > -- or http://example.com/ 'linked'"; | ||||||||||||
| 99 | $title = MKDoc::Text::Structured::Inline::process_entities_only ($title); | ||||||||||||
| 100 | |||||||||||||
| 101 | $title is now: | ||||||||||||
| 102 | |||||||||||||
| 103 | < My © symbol shouldn't be *bold* — > or http://example.com/ ‘linked’ | ||||||||||||
| 104 | |||||||||||||
| 105 | =cut | ||||||||||||
| 106 | |||||||||||||
| 107 | sub process_entities_only | ||||||||||||
| 108 | 2 | ▶ | { | ||||||||||
|
Subroutine
process_entities_only: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 109 | 2 | local $Text; | |||||||||||
| 110 | 2 | $Text = shift; | |||||||||||
| 111 | 2 | $Text = " $Text "; | |||||||||||
| 112 | 2 | $Text =~ s/\n/ /gsm; | |||||||||||
| 113 | |||||||||||||
| 114 | 2 | _make_entities(); | |||||||||||
| 115 | 2 | _make_simplequotes(); | |||||||||||
| 116 | 2 | _make_doublequotes(); | |||||||||||
| 117 | 2 | _break_long_words(); | |||||||||||
| 118 | |||||||||||||
| 119 | 2 | $Text =~ s/^ //; | |||||||||||
| 120 | 2 | $Text =~ s/ $//; | |||||||||||
| 121 | 2 | return $Text; | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | |||||||||||||
| 125 | sub _make_entities | ||||||||||||
| 126 | 95 | ▶ | { | ||||||||||
|
Subroutine
_make_entities: called CC=1 SCAR=0.0
| |||||||||||||
| 127 | 95 | $Text =~ s/&/&/g; | |||||||||||
| 128 | 95 | $Text =~ s/</</g; | |||||||||||
| 129 | 95 | $Text =~ s/>/>/g; | |||||||||||
| 130 | 95 | $Text =~ s/"/"/g; | |||||||||||
| 131 | |||||||||||||
| 132 | 95 | $Text =~ s/(?<=(?:\s|\n))--(?=(?:\s|\n))/\—/g; # -- becomes em-dash | |||||||||||
| 133 | 95 | $Text =~ s/(?<=(?:\s|\n))-(?=(?:\s|\n))/\–/g; # - becomes en-dash | |||||||||||
| 134 | 95 | $Text =~ s/(?<!\.)\.\.\.(?!\.)/\…/g; # ... becomes ellipsis | |||||||||||
| 135 | |||||||||||||
| 136 | 95 | $Text =~ s/\(tm\)(?=(?:\s|\n|\p{IsPunct}))/\™/gi; # (tm) becomes trademark | |||||||||||
| 137 | 95 | $Text =~ s/\(r\)(?=(?:\s|\n|\p{IsPunct}))/\®/gi; # (r) becomes registered | |||||||||||
| 138 | 95 | $Text =~ s/\(c\)(?=(?:\s|\n|\p{IsPunct}))/\©/gi; # (c) becomes copyright | |||||||||||
| 139 | 95 | $Text =~ s/(?<=(?:\s|\n))(\d+)\s*x\s*(\d+)(?=(?:\s|\n|\p{isPunct}))/$1\×$2/g; # x becomes dimension | |||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | |||||||||||||
| 143 | sub _make_abbr_implicit | ||||||||||||
| 144 | 2 | ▶ | { | ||||||||||
|
Subroutine
_make_abbr_implicit: called CC=1 SCAR=0.0
| |||||||||||||
| 145 | 2 | my $abbr = shift; | |||||||||||
| 146 | 2 | my $brack = shift; | |||||||||||
| 147 | 2 | my $title = $brack; | |||||||||||
| 148 | 2 | $title =~ s/^\s*\(\s*//; | |||||||||||
| 149 | 2 | $title =~ s/\s*\)\s*$//; | |||||||||||
| 150 | 2 | return qq|<abbr title="$title">$abbr</abbr> ($title)|; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | |||||||||||||
| 154 | sub _make_abbr_explicit | ||||||||||||
| 155 | 9 | ▶ | { | ||||||||||
|
Subroutine
_make_abbr_explicit: called CC=1 SCAR=0.0
| |||||||||||||
| 156 | 9 | my $abbr = shift; | |||||||||||
| 157 | 9 | my $brack = shift; | |||||||||||
| 158 | 9 | my $title = $brack; | |||||||||||
| 159 | 9 | $title =~ s/^\s*\(\s*//; | |||||||||||
| 160 | 9 | $title =~ s/\s*\)\s*$//; | |||||||||||
| 161 | 9 | return qq|<abbr title="$title">$abbr</abbr>|; | |||||||||||
| 162 | } | ||||||||||||
| 163 | |||||||||||||
| 164 | |||||||||||||
| 165 | sub _make_simplequotes | ||||||||||||
| 166 | 95 | ▶ | { | ||||||||||
|
Subroutine
_make_simplequotes: called CC=2 SCAR=6.9
| |||||||||||||
| 167 | $Text = join '', map { | ||||||||||||
| 168 | 95 | my $stuff = $_; | |||||||||||
| 169 | 179 | $stuff = " $stuff "; | |||||||||||
| 170 | 179 | ▶ | while ($stuff =~ s/ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | (?<=(?:\s|\n)) # must start with space or carriage return | ||||||||||||
| 172 | \' # simple quote | ||||||||||||
| 173 | ([^ \t\n\']|[^ \t\n\'].*?[^ \t\n\']) # stuff to capture and smart-quotize | ||||||||||||
| 174 | \' # simple quote | ||||||||||||
| 175 | (?=(?:<|\s|\n|\p{IsPunct}(?:\s|\n|<))) # must be followed by space, \n or (punctuation + space or \n) | ||||||||||||
| 176 | 6 | /_make_simplequotes_wrap ($1)/xes) {} | |||||||||||
| 177 | |||||||||||||
| 178 | 179 | $stuff =~ s/^ //; | |||||||||||
| 179 | 179 | $stuff =~ s/ $//; | |||||||||||
| 180 | 179 | $stuff; | |||||||||||
| 181 | } _tokenize ($Text); | ||||||||||||
| 182 | } | ||||||||||||
| 183 | |||||||||||||
| 184 | |||||||||||||
| 185 | sub _make_simplequotes_wrap | ||||||||||||
| 186 | 6 | ▶ | { | ||||||||||
|
Subroutine
_make_simplequotes_wrap: called CC=1 SCAR=0.0
| |||||||||||||
| 187 | 6 | my $stuff = shift; | |||||||||||
| 188 | 6 | local $Text = $stuff; | |||||||||||
| 189 | 6 | return "‘$Text’"; | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | |||||||||||||
| 193 | |||||||||||||
| 194 | sub _make_doublequotes | ||||||||||||
| 195 | 95 | ▶ | { | ||||||||||
|
Subroutine
_make_doublequotes: called CC=2 SCAR=6.9
| |||||||||||||
| 196 | $Text = join '', map { | ||||||||||||
| 197 | 95 | my $stuff = $_; | |||||||||||
| 198 | 179 | $stuff = " $stuff "; | |||||||||||
| 199 | 179 | $stuff =~ s/"/<QUOT>/g; | |||||||||||
| 200 | 179 | $stuff =~ s/"/"/g; | |||||||||||
| 201 | 179 | ▶ | while ($stuff =~ s/ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 202 | (?<=(?:\s|\n)) # must start with space or carriage return | ||||||||||||
| 203 | \" # double quote | ||||||||||||
| 204 | ([^ \t\n\"]|[^ \t\n\"].*?[^ \t\n\"]) # stuff to capture and smart-quotize | ||||||||||||
| 205 | \" # double quote | ||||||||||||
| 206 | (?=(?:<|\s|\n|\p{IsPunct}(?:\s|\n|<))) # must be followed by space, \n or (punctuation + space or \n) | ||||||||||||
| 207 | 6 | /_make_doublequotes_wrap ($1)/xes) {} | |||||||||||
| 208 | |||||||||||||
| 209 | 179 | $stuff =~ s/^ //; | |||||||||||
| 210 | 179 | $stuff =~ s/ $//; | |||||||||||
| 211 | 179 | $stuff =~ s/"/"/g; | |||||||||||
| 212 | 179 | $stuff =~ s/<QUOT>/"/g; | |||||||||||
| 213 | 179 | $stuff; | |||||||||||
| 214 | } _tokenize ($Text); | ||||||||||||
| 215 | } | ||||||||||||
| 216 | |||||||||||||
| 217 | |||||||||||||
| 218 | sub _make_doublequotes_wrap | ||||||||||||
| 219 | 6 | ▶ | { | ||||||||||
|
Subroutine
_make_doublequotes_wrap: called CC=1 SCAR=0.0
| |||||||||||||
| 220 | 6 | my $stuff = shift; | |||||||||||
| 221 | 6 | local $Text = $stuff; | |||||||||||
| 222 | 6 | return "“$Text”"; | |||||||||||
| 223 | } | ||||||||||||
| 224 | |||||||||||||
| 225 | |||||||||||||
| 226 | sub _make_strong | ||||||||||||
| 227 | 97 | ▶ | { | ||||||||||
|
Subroutine
_make_strong: called CC=2 SCAR=6.9
| |||||||||||||
| 228 | $Text = join '', map { | ||||||||||||
| 229 | 97 | my $stuff = $_; | |||||||||||
| 230 | 181 | $stuff = " $stuff "; | |||||||||||
| 231 | 181 | ▶ | while ($stuff =~ s/ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 232 | (?<=(?:\s|\n)) # must start with space or carriage return | ||||||||||||
| 233 | \* # star | ||||||||||||
| 234 | (\S|\S.*?\S) # stuff to capture and emphasize | ||||||||||||
| 235 | \* # star | ||||||||||||
| 236 | (?=(?:<|\s|\n|\p{IsPunct}(?:\s|\n|<))) # must be followed by space, \n or (punctuation + space or \n) | ||||||||||||
| 237 | 5 | /_make_strong_wrap ($1)/xes) {} | |||||||||||
| 238 | |||||||||||||
| 239 | 181 | $stuff =~ s/^ //; | |||||||||||
| 240 | 181 | $stuff =~ s/ $//; | |||||||||||
| 241 | 181 | $stuff; | |||||||||||
| 242 | } _tokenize ($Text); | ||||||||||||
| 243 | } | ||||||||||||
| 244 | |||||||||||||
| 245 | |||||||||||||
| 246 | sub _make_strong_wrap | ||||||||||||
| 247 | 5 | ▶ | { | ||||||||||
|
Subroutine
_make_strong_wrap: called CC=1 SCAR=0.0
| |||||||||||||
| 248 | 5 | my $stuff = shift; | |||||||||||
| 249 | 5 | local $Text = $stuff; | |||||||||||
| 250 | 5 | _make_em ($Text); | |||||||||||
| 251 | 5 | return "<strong>$Text</strong>"; | |||||||||||
| 252 | } | ||||||||||||
| 253 | |||||||||||||
| 254 | |||||||||||||
| 255 | sub _make_em | ||||||||||||
| 256 | 98 | ▶ | { | ||||||||||
|
Subroutine
_make_em: called CC=2 SCAR=6.9
| |||||||||||||
| 257 | $Text = join '', map { | ||||||||||||
| 258 | 98 | my $stuff = $_; | |||||||||||
| 259 | 202 | $stuff = " $stuff "; | |||||||||||
| 260 | 202 | ▶ | while ($stuff =~ s/ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 261 | (?<=(?:\s|\n)) # must start with space or carriage return | ||||||||||||
| 262 | _ # underscore | ||||||||||||
| 263 | (\S|\S.*?\S) # stuff to capture and emphasize | ||||||||||||
| 264 | _ # underscore | ||||||||||||
| 265 | (?=(?:<|\s|\n|\p{IsPunct}(?:\s|\n))) # must be followed by space, \n or (punctuation + space or \n) | ||||||||||||
| 266 | 4 | /_make_em_wrap ($1)/xes) {} | |||||||||||
| 267 | |||||||||||||
| 268 | 202 | $stuff =~ s/^ //; | |||||||||||
| 269 | 202 | $stuff =~ s/ $//; | |||||||||||
| 270 | 202 | $stuff; | |||||||||||
| 271 | } _tokenize ($Text); | ||||||||||||
| 272 | } | ||||||||||||
| 273 | |||||||||||||
| 274 | |||||||||||||
| 275 | sub _make_em_wrap | ||||||||||||
| 276 | 4 | ▶ | { | ||||||||||
|
Subroutine
_make_em_wrap: called CC=1 SCAR=0.0
| |||||||||||||
| 277 | 4 | my $stuff = shift; | |||||||||||
| 278 | |||||||||||||
| 279 | 4 | local $Text = $stuff; | |||||||||||
| 280 | 4 | _make_strong ($Text); | |||||||||||
| 281 | 4 | return "<em>$Text</em>"; | |||||||||||
| 282 | } | ||||||||||||
| 283 | |||||||||||||
| 284 | |||||||||||||
| 285 | sub _make_smilies | ||||||||||||
| 286 | 93 | ▶ | { | ||||||||||
|
Subroutine
_make_smilies: called CC=3 SCAR=11.0
| |||||||||||||
| 287 | $Text = join '', map { | ||||||||||||
| 288 | 93 | my $stuff = $_; | |||||||||||
| 289 | 213 | ▶ | $stuff =~ s/:-\)/<span class="smiley-happy">:-)<\/span>/g unless ($stuff =~ /^</); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 290 | 213 | ▶ | $stuff =~ s/:-\(/<span class="smiley-sad">:-(<\/span>/g unless ($stuff =~ /^</); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 291 | # don't do ;-) think about what happens with &-) | ||||||||||||
| 292 | 213 | $stuff; | |||||||||||
| 293 | } _tokenize ($Text); | ||||||||||||
| 294 | } | ||||||||||||
| 295 | |||||||||||||
| 296 | |||||||||||||
| 297 | sub _break_long_words | ||||||||||||
| 298 | 97 | ▶ | { | ||||||||||
|
Subroutine
_break_long_words: called CC=2 SCAR=6.9
| |||||||||||||
| 299 | $Text = join '', map { | ||||||||||||
| 300 | 97 | my $stuff = $_; | |||||||||||
| 301 | 241 | ▶ | $stuff = _insert_spaces ($stuff, $LongestWord) unless ($stuff =~ /^</); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 302 | 241 | $stuff; | |||||||||||
| 303 | } _tokenize ($Text); | ||||||||||||
| 304 | } | ||||||||||||
| 305 | |||||||||||||
| 306 | |||||||||||||
| 307 | sub _insert_spaces | ||||||||||||
| 308 | 172 | ▶ | { | ||||||||||
|
Subroutine
_insert_spaces: called CC=2 SCAR=6.9
| |||||||||||||
| 309 | 172 | my $text = shift; | |||||||||||
| 310 | 172 | ▶ | my $length = shift || return $text; | ||||||||||
|
Condition100%2/2
shift() || (return $text)
MC/DC100%1/1
shift() || (return $text)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 311 | # we can break continuous non-space text after "/", ";" or "-" | ||||||||||||
| 312 | 169 | $text =~ s/(\S{$length}[\/;-])(?=\S)/$1 /g; | |||||||||||
| 313 | # we can break continuous non-space text so long as it doesn't contain an ampersand | ||||||||||||
| 314 | 169 | $text =~ s/([^[:space:]&]{$length})(?=\S)/$1 /g; | |||||||||||
| 315 | 169 | return $text; | |||||||||||
| 316 | } | ||||||||||||
| 317 | |||||||||||||
| 318 | |||||||||||||
| 319 | sub _tokenize | ||||||||||||
| 320 | 575 | ▶ | { | ||||||||||
|
Subroutine
_tokenize: called CC=1 SCAR=0.0
| |||||||||||||
| 321 | 575 | my $text = shift; | |||||||||||
| 322 | 575 | my @res = $text =~ /([^<]+)|(<.+?>)/g; | |||||||||||
| 323 | 575 | return grep { defined $_ } @res; | |||||||||||
| 324 | } | ||||||||||||
| 325 | |||||||||||||
| 326 | |||||||||||||
| 327 | 21 | 1; | |||||||||||
| 328 | |||||||||||||
| 329 | |||||||||||||
| 330 | __END__ | ||||||||||||