blib/lib/SEO/Inspector.pm

filter: Statementstmt 65.4% 199 / 304 Branchbran 34.3% 44 / 128 Conditioncond 23.5% 12 / 51 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 96.6% 29 / 30 Podpod 100% 6 / 6 totaltotal 52.5% 290 / 552 CC 91 SCAR 39.8
CC
91
Cov
53%
_check_links
175.5
_check_readability
163.9
_fetch_html
20.0
CRAP 53.7
↑ Summary lib/SEO/Inspector/Plugin/MetaDescriptionCheck.pm »
1 package SEO::Inspector;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Module::Pluggable require => 1, search_path => 'SEO::Inspector::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Object::Configure 0.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Params::Get 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 SEO::Inspector - Run SEO checks on HTML or URLs
16  
17 =head1 VERSION
18  
19 Version 0.02
20  
21 =cut
22  
23 6 our $VERSION = '0.02';
24  
25 =head1 SYNOPSIS
26  
27 use SEO::Inspector;
28  
29 my $inspector = SEO::Inspector->new(url => 'https://example.com');
30  
31 # Run plugins
32 my $html = '<html><body>......</body></html>';
33 my $plugin_results = $inspector->check_html($html);
34  
35 # Run built-in checks
36 my $builtin_results = $inspector->run_all($html);
37  
38 # Check a single URL and get all results
39 my $all_results = $inspector->check_url('https://example.com');
40  
41 =head1 DESCRIPTION
42  
43 SEO::Inspector provides:
44  
45 =over 4
46  
47 =item * 14 built-in SEO checks
48  
49 =item * Plugin system: dynamically load modules under SEO::Inspector::Plugin namespace
50  
51 =item * Methods to check HTML strings or fetch and analyze a URL
52  
53 =back
54  
55 =head1 PLUGIN SYSTEM
56  
57 In addition to the built-in SEO checks, C<SEO::Inspector> supports a flexible
58 plugin system.
59 Plugins allow you to extend the checker with new rules or
60 specialized analysis without modifying the core module.
61  
62 =head2 How Plugins Are Found
63  
64 Plugins are loaded dynamically from the C<SEO::Inspector::Plugin> namespace.
65 For example, a module called:
66  
67 package SEO::Inspector::Plugin::MyCheck;
68  
69 will be detected and loaded automatically if it is available in C<@INC>.
70  
71 You can also tell the constructor to search additional directories by passing
72 the C<plugin_dirs> argument:
73  
74 my $inspector = SEO::Inspector->new(
75 plugin_dirs => ['t/lib', '/path/to/custom/plugins'],
76 );
77  
78 Each directory must contain files under a subpath corresponding to the
79 namespace, for example:
80  
81 /path/to/custom/plugins/SEO/Inspector/Plugin/Foo.pm
82  
83 =head2 Plugin Interface
84  
85 A plugin must provide at least two methods:
86  
87 =over 4
88  
89 =item * C<new>
90  
91 Constructor, called with no arguments.
92  
93 =item * C<run($html)>
94  
95 Given a string of raw HTML, return a hashref describing the result of the check.
96 The hashref should have at least these keys:
97  
98 {
99 name => 'My Check',
100 status => 'ok' | 'warn' | 'error',
101 notes => 'human-readable message',
102 resolution => 'how to resolve'
103 }
104  
105 =back
106  
107 =head2 Running Plugins
108  
109 You can run all loaded plugins against a piece of HTML with:
110  
111 my $results = $inspector->check_html($html);
112  
113 This returns a hashref keyed by plugin name (lowercased), each value being the
114 hashref returned by the plugin's C<run> method.
115  
116 Plugins are also run automatically when you call C<check_url>:
117  
118 my $results = $inspector->check_url('https://example.com');
119  
120 That result will include both built-in checks and plugin checks.
121  
122 =head2 Example Plugin
123  
124 Here is a minimal example plugin that checks whether the page contains
125 the string "Hello":
126  
127 package SEO::Inspector::Plugin::HelloCheck;
128 use strict;
129 use warnings;
130  
131 sub new { bless {}, shift }
132  
133 sub run {
134 my ($self, $html) = @_;
135 if($html =~ /Hello/) {
136 return { name => 'Hello Check', status => 'ok', notes => 'found Hello' };
137 } else {
138 return { name => 'Hello Check', status => 'warn', notes => 'no Hello', resolution => 'add a hello field' };
139 }
140 }
141  
142 1;
143  
144 Place this file under C<lib/SEO/Inspector/Plugin/HelloCheck.pm> (or another
145 directory listed in C<plugin_dirs>), and it will be discovered automatically.
146  
147 =head2 Naming Conventions
148  
149 The plugin key stored in C<< $inspector->{plugins} >> is derived from the final
150 part of the package name, lowercased. For example:
151  
152 SEO::Inspector::Plugin::HelloCheck -> "hellocheck"
153  
154 This is the key you will see in the hashref returned by C<check_html> or
155 C<check_url>.
156  
157 =head1 METHODS
158  
159 =head2 new(%args)
160  
161 Create a new inspector object. Accepts optional C<url> and C<plugin_dirs> arguments.
162 If C<plugin_dirs> isn't given, it tries hard to find the right place.
163  
164 =cut
165  
166 # -------------------------------
167 # Constructor
168 # -------------------------------
169 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
170 5 my $class = shift;
171 5 my $params = Object::Configure::configure($class, Params::Get::get_params(undef, \@_));
172  
173 5 ▶ $params->{'ua'} ||= Mojo::UserAgent->new();
Condition33%1/3
$params->{"ua"} ||= "Mojo::UserAgent"->new
l!l&&r!l&&!r
050
MC/DC0%0/2
$params->{"ua"} ||= "Mojo::UserAgent"->new
$params->{"ua"} "Mojo::UserAgent"->new
Truth table33%1/3
A ||= B
A= $params->{"ua"}B= "Mojo::UserAgent"->new
ABresult
1X1
011
000
174 5 ▶ $params->{'plugins'} ||= {};
Condition50%1/2
$params->{"plugins"} ||= {}
l!l
05
MC/DC0%0/1
$params->{"plugins"} ||= {}
$params->{"plugins"}
Truth table50%1/2
A
A= $params->{"plugins"}
Aresult
11
00
175  
176 5 my $self = bless { %{$params} }, $class;
177  
178 5 $self->load_plugins();
179  
180 5 return $self;
181 }
182  
183 =head2 load_plugins
184  
185 Loads plugins from the C<SEO::Inspector::Plugin> namespace.
186  
187 =cut
188  
189 # -------------------------------
190 # Load plugins from SEO::Inspector::Plugin namespace
191 # -------------------------------
192 5 ▶sub load_plugins {
Subroutine load_plugins: called CC=5 SCAR=16.1
Pod documented
193 5 my $self = $_[0];
194  
195 5 for my $plugin ($self->plugins()) {
196 13 my $key = lc($plugin =~ s/.*:://r);
197 13 $self->{plugins}{$key} = $plugin->new();
198 }
199 5 ▶ if($self->{plugin_dirs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"plugin_dirs"}) 2 3 5
200 2 for my $dir (@{$self->{plugin_dirs}}) {
201 2 local @INC = ($dir, @INC);
202  
203 2 my $finder = Module::Pluggable::Object->new(
204 search_path => ['SEO::Inspector::Plugin'],
205 require => 1,
206 instantiate => 'new',
207 );
208  
209 2 for my $plugin ($finder->plugins) {
210 6 my $key = lc(ref($plugin) =~ s/.*:://r);
211 6 $self->{plugins}{$key} = $plugin;
212 }
213 }
214 }
215 }
216  
217 # -------------------------------
218 # Fetch HTML from URL or object default
219 # -------------------------------
220 0 ▶sub _fetch_html {
Subroutine _fetch_html: not called CC=4 SCAR=30.0
221 0 my ($self, $url) = @_;
222 0 ▶ $url //= $self->{url};
Condition0%0/3
$url //= $self->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$url //= $self->{"url"}
$url $self->{"url"}
Truth table0%0/3
A //= B
A= $urlB= $self->{"url"}
ABresult
1X1
011
000
223 0 ▶ croak 'URL missing' unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
224  
225 0 my $res = $self->{ua}->get($url)->result;
226 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
227 0 croak 'Fetch failed: ', $res->message();
228 }
229 0 return $res->body;
230 }
231  
232 =head2 check($check_name, $html)
233  
234 Run a single built-in check or plugin on provided HTML (or fetch from object URL if HTML not provided).
235  
236 =cut
237  
238 # -------------------------------
239 # Run a single plugin or built-in check
240 # -------------------------------
241 60 ▶sub check {
Subroutine check: called CC=4 SCAR=19.8
Pod documented
242 60 my ($self, $check_name, $html) = @_;
243 60 ▶ $html //= $self->_fetch_html();
Condition33%1/3
$html //= $self->_fetch_html
l!l&&r!l&&!r
6000
MC/DC0%0/2
$html //= $self->_fetch_html
$html $self->_fetch_html
Truth table33%1/3
A //= B
A= $htmlB= $self->_fetch_html
ABresult
1X1
011
000
244  
245 60 my %dispatch = (
246 title => \&_check_title,
247 meta_description => \&_check_meta_description,
248 canonical => \&_check_canonical,
249 robots_meta => \&_check_robots_meta,
250 viewport => \&_check_viewport,
251 h1_presence => \&_check_h1_presence,
252 word_count => \&_check_word_count,
253 links_alt_text => \&_check_links_alt_text,
254 check_structured_data => \&_check_structured_data,
255 check_headings => \&_check_headings,
256 check_links => \&_check_links,
257 open_graph => \&_check_open_graph,
258 twitter_cards => \&_check_twitter_cards,
259 page_size => \&_check_page_size,
260 readability => \&_check_readability,
261 );
262  
263 # built-in checks
264 60 ▶ if (exists $dispatch{$check_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dispatch{$check_name}) { } 60 0 60
265 60 return $dispatch{$check_name}->($self, $html);
266 } else {
267 0 croak "Unknown check $check_name";
268 }
269  
270 # plugin checks
271 0 ▶ if (exists $self->{plugins}{$check_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"plugins"}{$check_name}) 0 0 0
272 0 my $plugin = $self->{plugins}{$check_name};
273 0 return $plugin->run($html);
274 }
275  
276 0 return { name => $check_name, status => 'unknown', notes => '' };
277 }
278  
279 =head2 run_all($html)
280  
281 Run all built-in checks on HTML (or object URL).
282  
283 =cut
284  
285 # -------------------------------
286 # Run all built-in checks
287 # -------------------------------
288 sub run_all
289 4 ▶{
Subroutine run_all: called CC=3 SCAR=11.3
Pod documented
290 4 my ($self, $html) = @_;
291 4 ▶ $html //= $self->_fetch_html();
Condition33%1/3
$html //= $self->_fetch_html
l!l&&r!l&&!r
400
MC/DC0%0/2
$html //= $self->_fetch_html
$html $self->_fetch_html
Truth table33%1/3
A //= B
A= $htmlB= $self->_fetch_html
ABresult
1X1
011
000
292  
293 4 my %results;
294 4 for my $check (qw(
295 title meta_description canonical robots_meta viewport h1_presence word_count links_alt_text
296 check_structured_data check_headings check_links
297 open_graph twitter_cards page_size readability
298 )) {
299 60 $results{$check} = $self->check($check, $html);
300 }
301  
302 4 return \%results;
303 }
304  
305 =head2 check_html($html)
306  
307 Run all loaded plugins on HTML.
308  
309 =cut
310  
311 # -------------------------------
312 # Run all plugins on HTML
313 # -------------------------------
314 5 ▶sub check_html {
Subroutine check_html: called CC=3 SCAR=11.2
Pod documented
315 5 my ($self, $html) = @_;
316 5 ▶ $html //= $self->_fetch_html();
Condition33%1/3
$html //= $self->_fetch_html
l!l&&r!l&&!r
500
MC/DC0%0/2
$html //= $self->_fetch_html
$html $self->_fetch_html
Truth table33%1/3
A //= B
A= $htmlB= $self->_fetch_html
ABresult
1X1
011
000
317 5 my %results;
318  
319 5 for my $key (keys %{ $self->{plugins} }) {
320 15 my $plugin = $self->{plugins}{$key};
321 15 $results{$key} = $plugin->run($html);
322 }
323  
324 5 return \%results;
325 }
326  
327 =head2 check_url($url)
328  
329 Fetch the URL and run all plugins and built-in checks.
330  
331 =cut
332  
333 # -------------------------------
334 # Run URL: fetch and check
335 # -------------------------------
336 1 ▶sub check_url {
Subroutine check_url: called CC=3 SCAR=11.3
Pod documented
337 1 my ($self, $url) = @_;
338  
339 1 ▶ $url //= $self->{url};
Condition33%1/3
$url //= $self->{"url"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$url //= $self->{"url"}
$url $self->{"url"}
Truth table33%1/3
A //= B
A= $urlB= $self->{"url"}
ABresult
1X1
011
000
340  
341 1 ▶ croak('URL missing') unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 1 1
342  
343 1 my $html = $self->_fetch_html($url);
344  
345 1 my $plugin_results = $self->check_html($html);
346 1 my $builtin_results = $self->run_all($html);
347  
348 # merge all results
349 1 my %results = (%$plugin_results, %$builtin_results, _html => $html);
350 1 return \%results;
351 }
352  
353 # -------------------------------
354 # Built-in check implementations
355 # -------------------------------
356 4 ▶sub _check_title {
Subroutine _check_title: called CC=6 SCAR=21.9
357 4 my ($self, $html) = @_;
358  
359 4 ▶ if ($html =~ /<title>(.*?)<\/title>/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<title>(.*?)</title>]is) 4 0 4
360 4 my $title = $1;
361 4 $title =~ s/^\s+|\s+$//g; # trim
362 4 $title =~ s/\s{2,}/ /g; # collapse spaces
363  
364 4 my $len = length($title);
365 4 my $status = 'ok';
366 4 my $notes = "title present ($len chars)";
367  
368 4 ▶ if ($len == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($len == 0) { } 0 4 4
elsif ($len < 10) { } 4 0 4
elsif ($len > 65) { } 0 0 0
369 0 $status = 'error';
370 0 $notes = 'empty title';
371 } elsif ($len < 10) {
372 4 $status = 'warn';
373 4 $notes = "title too short ($len chars)";
374 } elsif ($len > 65) {
375 0 $status = 'warn';
376 0 $notes = "title too long ($len chars)";
377 }
378  
379 # Flag really weak titles
380 4 ▶ if ($title =~ /^(home|untitled|index)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ /^(home|untitled|index)$/i) 0 4 4
381 0 $status = 'warn';
382 0 $notes = "generic title: $title";
383 }
384  
385 4 return { name => 'Title', status => $status, notes => $notes };
386 }
387  
388 0 return { name => 'Title', status => 'error', notes => 'missing title' };
389 }
390  
391  
392 4 ▶sub _check_meta_description {
Subroutine _check_meta_description: called CC=2 SCAR=6.9
393 4 my ($self, $html) = @_;
394 4 ▶ if ($html =~ /<meta\s+name=["']description["']\s+content=["'](.*?)["']/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<meta\s+name=["']description["']\s+content=["'](.*?)["']/is) 3 1 4
395 3 my $desc = $1;
396 3 return { name => 'Meta Description', status => 'ok', notes => 'meta description present' };
397 }
398 1 return { name => 'Meta Description', status => 'warn', notes => 'missing meta description' };
399 }
400  
401 sub _check_canonical
402 4 ▶{
Subroutine _check_canonical: called CC=2 SCAR=7.6
403 4 my ($self, $html) = @_;
404 4 ▶ if ($html =~ /<link\s+rel=["']canonical["']\s+href=["'](.*?)["']/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<link\s+rel=["']canonical["']\s+href=["'](.*?)["']/is) 0 4 4
405 0 return { name => 'Canonical', status => 'ok', notes => 'canonical link present' };
406 }
407 return {
408 4 name => 'Canonical',
409 status => 'warn',
410 notes => 'missing canonical link',
411 resolution => 'Add canonical link to <head>: <link rel="canonical" href="https://your-domain.com/this-page-url"> - use the preferred URL for this page to prevent duplicate content issues'
412 };
413 }
414  
415 4 ▶sub _check_robots_meta {
Subroutine _check_robots_meta: called CC=2 SCAR=7.6
416 4 my ($self, $html) = @_;
417 4 ▶ if ($html =~ /<meta\s+name=["']robots["']\s+content=["'](.*?)["']/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<meta\s+name=["']robots["']\s+content=["'](.*?)["']/is) 0 4 4
418 0 return { name => 'Robots Meta', status => 'ok', notes => 'robots meta present' };
419 }
420 return {
421 4 name => 'Robots Meta',
422 status => 'warn',
423 notes => 'missing robots meta',
424 resolution => 'Add robots meta tag to <head>: <meta name="robots" content="index, follow"> for normal indexing, or <meta name="robots" content="noindex, nofollow"> to prevent indexing - controls how search engines crawl and index this page'
425 };
426 }
427  
428 4 ▶sub _check_viewport {
Subroutine _check_viewport: called CC=2 SCAR=7.6
429 4 my ($self, $html) = @_;
430 4 ▶ if ($html =~ /<meta\s+name=["']viewport["']\s+content=["'](.*?)["']/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<meta\s+name=["']viewport["']\s+content=["'](.*?)["']/is) 0 4 4
431 0 return { name => 'Viewport', status => 'ok', notes => 'viewport meta present' };
432 }
433 return {
434 4 name => 'Viewport',
435 status => 'warn',
436 notes => 'missing viewport meta',
437 resolution => 'Add viewport meta tag to <head>: <meta name="viewport" content="width=device-width, initial-scale=1.0"> - essential for mobile responsiveness and Google mobile-first indexing'
438 };
439 }
440  
441 4 ▶sub _check_h1_presence {
Subroutine _check_h1_presence: called CC=2 SCAR=6.9
442 4 my ($self, $html) = @_;
443 4 ▶ if ($html =~ /<h1\b[^>]*>(.*?)<\/h1>/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<h1\b[^>]*>(.*?)</h1>]is) 3 1 4
444 3 return { name => 'H1 Presence', status => 'ok', notes => 'h1 tag present' };
445 }
446 1 return { name => 'H1 Presence', status => 'warn', notes => 'missing h1' };
447 }
448  
449 4 ▶sub _check_word_count {
Subroutine _check_word_count: called CC=2 SCAR=7.0
450 4 my ($self, $html) = @_;
451 4 my $text = $html;
452 4 $text =~ s/<[^>]+>//g;
453 4 my $words = scalar split /\s+/, $text;
454 4 ▶ return { name => 'Word Count', status => $words > 0 ? 'ok' : 'warn', notes => "$words words" };
Branch: 1 branch
BranchTrueFalseTotal
$words > 0 ? : 4 0 4
455 }
456  
457 4 ▶sub _check_links_alt_text {
Subroutine _check_links_alt_text: called CC=4 SCAR=17.9
458 4 my ($self, $html) = @_;
459 4 my @missing;
460 4 ▶ while ($html =~ /<img\b(.*?)>/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<img\b(.*?)>/sig) 0 4 4
461 0 my $attr = $1;
462 0 ▶ push @missing, $1 unless $attr =~ /alt=/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /alt=/i 0 0 0
463 }
464 4 ▶ if(scalar(@missing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @missing) 0 4 4
465 return {
466 0 name => 'Links Alt Text',
467 status => 'warn',
468 notes => scalar(@missing) . ' images missing alt',
469 resolution => 'Add alt attributes to all images: <img src="image.jpg" alt="Descriptive text"> - describe the image content for screen readers and SEO. Use alt="" for decorative images that don\'t add meaning'
470 };
471 }
472  
473 return {
474 4 name => 'Links Alt Text',
475 status => 'ok',
476 notes => 'all images have alt'
477 };
478 }
479  
480 4 ▶sub _check_structured_data {
Subroutine _check_structured_data: called CC=2 SCAR=7.4
481 4 my ($self, $html) = @_;
482  
483 4 my @jsonld = ($html =~ /<script\b[^>]*type=["']application\/ld\+json["'][^>]*>(.*?)<\/script>/gis);
484  
485 4 ▶ if(scalar(@jsonld)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @jsonld) 0 4 4
486 return {
487 0 name => 'Structured Data',
488 status => 'ok',
489 notes => scalar(@jsonld) . ' JSON-LD block(s) found'
490 };
491 }
492  
493 return {
494 4 name => 'Structured Data',
495 status => 'warn',
496 notes => 'no structured data found',
497 resolution => 'Add JSON-LD structured data to <head>: <script type="application/ld+json">{"@context": "https://schema.org", "@type": "WebPage", "name": "Page Title", "description": "Page description"}</script> - helps search engines understand your content better and enables rich snippets'
498 }
499 }
500  
501 # _check_headings
502 # ----------------
503 # Analyzes the HTML document for heading structure and returns a structured
504 # SEO/a11y report.
505 #
506 # Checks performed:
507 # - Presence of headings (<h1>–<h6>), with counts of each level.
508 # - Ensures exactly one <h1> exists (warns if missing or multiple).
509 # - Validates heading hierarchy (no skipped levels, e.g. <h3> should not appear before an <h2>).
510 # - Flags suspicious heading text lengths (too short < 2 chars, or too long > 120 chars).
511 #
512 # Returns:
513 # {
514 # name => 'Headings',
515 # status => 'ok' | 'warn',
516 # notes => 'summary of counts and issues'
517 # }
518 #
519 # Notes:
520 # - Status is 'warn' if issues are found, otherwise 'ok'.
521 # - 'error' status is reserved for future use (currently unused).
522  
523 4 ▶sub _check_headings {
Subroutine _check_headings: called CC=13 SCAR=29.6
524 4 my ($self, $html) = @_;
525  
526 4 my %counts;
527 my @headings;
528  
529 # Capture all headings and their order
530 4 ▶ while ($html =~ /<(h[1-6])\b[^>]*>(.*?)<\/\1>/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<(h[1-6])\b[^>]*>(.*?)</\1>]gi) 3 4 7
531 3 my $tag = lc $1;
532 3 ▶ my $text = $2 // '';
Condition50%1/2
$2 // ""
l!l
30
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
533 3 $text =~ s/\s+/ /g; # normalize whitespace
534 3 $text =~ s/^\s+|\s+$//g;
535  
536 3 $counts{$tag}++;
537 3 push @headings, { level => substr($tag, 1), text => $text };
538 }
539  
540 4 my @issues;
541 4 my $status = 'ok';
542  
543 # Check for no headings
544 4 ▶ if (!%counts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%counts) 1 3 4
545 return {
546 1 name => 'Headings',
547 status => 'warn',
548 notes => 'no headings found',
549 };
550 }
551  
552 # Check H1 presence/uniqueness
553 3 ▶ if (!$counts{h1}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $counts{'h1'}) { } 0 3 3
elsif ($counts{'h1'} > 1) { } 0 3 3
554 0 push @issues, 'missing <h1>';
555 0 $status = 'warn';
556 }
557 elsif ($counts{h1} > 1) {
558 0 push @issues, 'multiple <h1> tags';
559 0 $status = 'warn';
560 }
561  
562 # Check heading hierarchy (no skipped levels)
563 3 my $last_level = 0;
564 3 for my $h (@headings) {
565 3 my $level = $h->{level};
566 3 ▶ if ($last_level && $level > $last_level + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_level and $level > $last_level + 1) 0 3 3
Condition33%1/3
$last_level and $level > $last_level + 1
!ll&&!rl&&r
300
MC/DC0%0/2
$last_level and $level > $last_level + 1
$last_level $level > $last_level + 1
Truth table33%1/3
A and B
A= $last_levelB= $level > $last_level + 1
ABresult
0X0
100
111
567 0 push @issues, "skipped heading level before <h$level>";
568 0 $status = 'warn';
569 }
570 3 $last_level = $level;
571 }
572  
573 # Check heading text length (too short or too long)
574 3 for my $h (@headings) {
575 3 my $len = length($h->{text});
576 3 ▶ if ($len < 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < 2) { } 0 3 3
elsif ($len > 120) { } 0 3 3
577 0 push @issues, "<h$h->{level}> too short";
578 0 $status = 'warn';
579 }
580 elsif ($len > 120) {
581 0 push @issues, "<h$h->{level}> too long";
582 0 $status = 'warn';
583 }
584 }
585  
586 # Summarize counts and issues
587 3 my $summary = join ', ', map { "$_: $counts{$_}" } sort keys %counts;
588 3 ▶ $summary .= @issues ? " | Issues: " . join('; ', @issues) : '';
Branch: 1 branch
BranchTrueFalseTotal
@issues ? : 0 3 3
589  
590 return {
591 3 name => 'Headings',
592 status => $status,
593 notes => $summary,
594 };
595 }
596  
597 4 ▶sub _check_links {
Subroutine _check_links: called CC=19 SCAR=51.7
598 4 my ($self, $html) = @_;
599  
600 4 my $base_host;
601 4 ▶ if ($self->{url} && $self->{url} =~ m{^https?://}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"url"} and $self->{"url"} =~ m[^https?://]i) 0 4 4
Condition33%1/3
$self->{"url"} and $self->{"url"} =~ m[^https?://]i
!ll&&!rl&&r
400
MC/DC0%0/2
$self->{"url"} and $self->{"url"} =~ m[^https?://]i
$self->{"url"} $self->{"url"} =~ m[^https?://]i
Truth table33%1/3
A and B
A= $self->{"url"}B= $self->{"url"} =~ m[^https?://]i
ABresult
0X0
100
111
602 0 $base_host = Mojo::URL->new($self->{url})->host;
603 }
604  
605 4 my ($total, $internal, $external, $badtext) = (0,0,0,0);
606  
607 # common "bad" link text patterns (exact match or just punctuation around)
608 4 my $bad_rx = qr/^(?:click\s*here|read\s*more|more|link|here|details)$/i;
609  
610 4 ▶ while ($html =~ m{<a\b([^>]*)>(.*?)</a>}gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<a\b([^>]*)>(.*?)</a>]sig) 0 4 4
611 0 my $attrs = $1;
612 0 ▶ my $text = $2 // '';
Condition0%0/2
$2 // ""
l!l
00
MC/DC0%0/1
$2 // ""
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
613  
614 0 $total++;
615  
616 # get href (prefer quoted values)
617 0 my ($href) = $attrs =~ /\bhref\s*=\s*"(.*?)"/i;
618 0 ▶ $href //= ($attrs =~ /\bhref\s*=\s*'(.*?)'/i ? $1 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$attrs =~ /\bhref\s*=\s*'(.*?)'/i ? : 0 0 0
Condition0%0/3
$href //= $attrs =~ /\bhref\s*=\s*'(.*?)'/i ? $1 : undef
l!l&&r!l&&!r
000
MC/DC0%0/2
$href //= $attrs =~ /\bhref\s*=\s*'(.*?)'/i ? $1 : undef
$href $attrs =~ /\bhref\s*=\s*'(.*?)'/i ? $1 : undef
Truth table0%0/3
A //= B
A= $hrefB= $attrs =~ /\bhref\s*=\s*'(.*?)'/i ? $1 : undef
ABresult
1X1
011
000
619 0 ▶ $href //= ($attrs =~ /\bhref\s*=\s*([^\s>]+)/i ? $1 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$attrs =~ /\bhref\s*=\s*([^\s>]+)/i ? : 0 0 0
Condition0%0/3
$href //= $attrs =~ /\bhref\s*=\s*([^\s>]+)/i ? $1 : undef
l!l&&r!l&&!r
000
MC/DC0%0/2
$href //= $attrs =~ /\bhref\s*=\s*([^\s>]+)/i ? $1 : undef
$href $attrs =~ /\bhref\s*=\s*([^\s>]+)/i ? $1 : undef
Truth table0%0/3
A //= B
A= $hrefB= $attrs =~ /\bhref\s*=\s*([^\s>]+)/i ? $1 : undef
ABresult
1X1
011
000
620  
621 # classify internal vs external
622 0 ▶ if (defined $href && $href =~ m{^\s*https?://}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $href and $href =~ m[^\s*https?://]i) { } 0 0 0
Condition0%0/3
defined $href and $href =~ m[^\s*https?://]i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $href and $href =~ m[^\s*https?://]i
defined $href $href =~ m[^\s*https?://]i
Truth table0%0/3
A and B
A= defined $hrefB= $href =~ m[^\s*https?://]i
ABresult
0X0
100
111
623 # attempt to compare host
624 0 my ($host) = $href =~ m{^\s*https?://([^/:\s]+)}i;
625 0 ▶ if (defined $base_host && defined $host) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base_host and defined $host) { } 0 0 0
Condition0%0/3
defined $base_host and defined $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $base_host and defined $host
defined $base_host defined $host
Truth table0%0/3
A and B
A= defined $base_hostB= defined $host
ABresult
0X0
100
111
626 0 ▶ if (lc $host eq lc $base_host) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $host eq lc $base_host) { } 0 0 0
627 0 $internal++;
628 } else {
629 0 $external++;
630 }
631 } else {
632 # no base host to compare; treat as external if absolute URL
633 0 $external++;
634 }
635 } else {
636 # relative URL or fragment or mailto/etc -> treat as internal
637 0 $internal++;
638 }
639  
640 # normalize visible text: strip tags, trim whitespace, collapse spaces
641 0 $text =~ s/<[^>]+>//g;
642 0 $text =~ s/^\s+|\s+$//g;
643 0 $text =~ s/\s+/ /g;
644  
645 # check for bad link text (exact-ish)
646 0 ▶ if ($text =~ $bad_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$bad_rx/) 0 0 0
647 0 $badtext++;
648 }
649 }
650  
651 4 ▶ my $status = ($external || $badtext) ? 'warn' : ($total ? 'ok' : 'warn');
Branch: 2 branches
BranchTrueFalseTotal
$external || $badtext ? : 0 4 4
$total ? : 0 4 4
Condition33%1/3
$external or $badtext
l!l&&r!l&&!r
004
MC/DC0%0/2
$external or $badtext
$external $badtext
Truth table33%1/3
A or B
A= $externalB= $badtext
ABresult
1X1
011
000
652  
653 4 my $notes;
654 4 ▶ if ($total) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total) { } 0 4 4
655 0 $notes = sprintf("%d total (%d internal, %d external). %d link(s) with poor anchor text",
656 $total, $internal, $external, $badtext);
657 } else {
658 4 $notes = 'no links found';
659 }
660  
661 return {
662 4 name => 'Links',
663 status => $status,
664 notes => $notes,
665 };
666 }
667  
668 # Checks for essential Open Graph tags that improve social media sharing
669 4 ▶sub _check_open_graph {
Subroutine _check_open_graph: called CC=6 SCAR=20.6
670 4 my ($self, $html) = @_;
671  
672 4 my %og_tags;
673 4 my @required = qw(title description image url);
674  
675 # Extract all Open Graph meta tags
676 4 ▶ while ($html =~ /<meta\s+(?:property|name)=["']og:([^"']+)["']\s+content=["']([^"']*)["']/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<meta\s+(?:property|name)=["']og:([^"']+)["']\s+content=["']([^"']*)["']/sig) 0 4 4
677 0 $og_tags{$1} = $2;
678 }
679  
680 4 ▶ my @missing = grep { !exists $og_tags{$_} || !$og_tags{$_} } @required;
Condition33%1/3
exists $og_tags{$_} && $og_tags{$_}
!ll&&!rl&&r
1600
MC/DC0%0/2
exists $og_tags{$_} && $og_tags{$_}
exists $og_tags{$_} $og_tags{$_}
Truth table33%1/3
A && B
A= exists $og_tags{$_}B= $og_tags{$_}
ABresult
0X0
100
111
681 4 my $found = keys %og_tags;
682  
683 4 ▶ my $status = @missing ? 'warn' : 'ok';
Branch: 1 branch
BranchTrueFalseTotal
@missing ? : 4 0 4
684 4 my $notes;
685  
686 4 ▶ if ($found == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found == 0) { } 4 0 4
elsif (@missing) { } 0 0 0
687 4 $notes = 'no Open Graph tags found';
688 4 $status = 'warn';
689 } elsif (@missing) {
690 0 $notes = sprintf('%d OG tags found, missing: %s', $found, join(', ', @missing));
691 } else {
692 0 $notes = sprintf('all essential OG tags present (%d total)', $found);
693 }
694  
695 return {
696 4 name => 'Open Graph',
697 status => $status,
698 notes => $notes,
699 resolution => 'Add missing tags to <head>: <meta property="og:title" content="Your Page Title">, <meta property="og:description" content="Brief page description">'
700 };
701 }
702  
703 # Checks for Twitter Card meta tags for better Twitter sharing
704 4 ▶sub _check_twitter_cards {
Subroutine _check_twitter_cards: called CC=6 SCAR=20.6
705 4 my ($self, $html) = @_;
706  
707 4 my %twitter_tags;
708 4 my @recommended = qw(card title description);
709  
710 # Extract Twitter Card meta tags
711 4 ▶ while ($html =~ /<meta\s+(?:property|name)=["']twitter:([^"']+)["']\s+content=["']([^"']*)["']/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<meta\s+(?:property|name)=["']twitter:([^"']+)["']\s+content=["']([^"']*)["']/sig) 0 4 4
712 0 $twitter_tags{$1} = $2;
713 }
714  
715 4 ▶ my @missing = grep { !exists $twitter_tags{$_} || !$twitter_tags{$_} } @recommended;
Condition33%1/3
exists $twitter_tags{$_} && $twitter_tags{$_}
!ll&&!rl&&r
1200
MC/DC0%0/2
exists $twitter_tags{$_} && $twitter_tags{$_}
exists $twitter_tags{$_} $twitter_tags{$_}
Truth table33%1/3
A && B
A= exists $twitter_tags{$_}B= $twitter_tags{$_}
ABresult
0X0
100
111
716 4 my $found = keys %twitter_tags;
717  
718 4 ▶ my $status = @missing ? 'warn' : 'ok';
Branch: 1 branch
BranchTrueFalseTotal
@missing ? : 4 0 4
719 4 my $notes;
720  
721 4 ▶ if ($found == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found == 0) { } 4 0 4
elsif (@missing) { } 0 0 0
722 4 $notes = 'no Twitter Card tags found';
723 4 $status = 'warn';
724 } elsif (@missing) {
725 0 $notes = sprintf('%d Twitter tags found, missing: %s', $found, join(', ', @missing));
726 } else {
727 0 $notes = sprintf('essential Twitter Card tags present (%d total)', $found);
728 }
729  
730 return {
731 4 name => 'Twitter Cards',
732 status => $status,
733 notes => $notes,
734 resolution => 'Add missing tags to <head>: <meta name="twitter:card" content="summary">, <meta name="twitter:title" content="Your Page Title">'
735 };
736 }
737  
738 # Checks HTML size and warns if too large (impacts loading speed)
739 4 ▶sub _check_page_size {
Subroutine _check_page_size: called CC=4 SCAR=16.3
740 4 my ($self, $html) = @_;
741  
742 4 my $size_bytes = length($html);
743 4 my $size_kb = int($size_bytes / 1024);
744  
745 4 my $status = 'ok';
746 4 my $notes = "${size_kb}KB HTML size";
747 4 my $resolution = '';
748  
749 4 ▶ if ($size_bytes > 1_048_576) { # > 1MB
Branch: 3 branches
BranchTrueFalseTotal
if ($size_bytes > 1048576) { } 0 4 4
elsif ($size_bytes > 102400) { } 0 4 4
elsif ($size_bytes < 1024) { } 4 0 4
750 0 $status = 'error';
751 0 $notes .= ' (too large, over 1MB)';
752 0 $resolution = 'Consider optimizing: minify CSS/JS, compress images, remove unused elements, enable server compression';
753 } elsif ($size_bytes > 102_400) { # > 100KB
754 0 $status = 'warn';
755 0 $notes .= ' (large, consider optimization)';
756 } elsif ($size_bytes < 1024) { # < 1KB
757 4 $status = 'warn';
758 4 $notes .= ' (suspiciously small)';
759 } else {
760 0 $notes .= ' (good size)';
761 }
762  
763 return {
764 4 name => 'Page Size',
765 status => $status,
766 notes => $notes,
767 resolution => $resolution
768 };
769 }
770  
771 # Calculates approximate Flesch Reading Ease score for content readability
772 4 ▶sub _check_readability {
Subroutine _check_readability: called CC=15 SCAR=51.0
773 4 my ($self, $html) = @_;
774  
775 # Extract text content (remove scripts, styles, and HTML tags)
776 4 my $text = $html;
777 4 $text =~ s/<script\b[^>]*>.*?<\/script>//gis;
778 4 $text =~ s/<style\b[^>]*>.*?<\/style>//gis;
779 4 $text =~ s/<[^>]+>//g;
780 4 $text =~ s/\s+/ /g;
781 4 $text =~ s/^\s+|\s+$//g;
782  
783 return {
784 4 ▶ name => 'Readability',
Branch: 1 branch
BranchTrueFalseTotal
if length $text < 100 4 0 4
785 status => 'warn',
786 notes => 'insufficient text for analysis',
787 resolution => 'Add more content to the page - aim for at least 300 words of meaningful text',
788 } if length($text) < 100;
789  
790 # Count sentences (approximate)
791 0 my $sentences = () = $text =~ /[.!?]+/g;
792 0 ▶ $sentences = 1 if $sentences == 0; # avoid division by zero
Branch: 1 branch
BranchTrueFalseTotal
if $sentences == 0 0 0 0
793  
794 # Count words
795 0 my @words = split /\s+/, $text;
796 0 my $word_count = @words;
797  
798 return {
799 0 ▶ name => 'Readability',
Branch: 1 branch
BranchTrueFalseTotal
if $word_count < 50 0 0 0
800 status => 'warn',
801 notes => 'insufficient content for analysis',
802 resolution => 'Add more substantial content - aim for at least 300 words for proper SEO value',
803 } if $word_count < 50;
804  
805 # Count syllables (very basic approximation)
806 0 my $syllables = 0;
807 0 for my $word (@words) {
808 0 $word = lc($word);
809 0 $word =~ s/[^a-z]//g; # remove punctuation
810 0 ▶ next if length($word) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $word == 0 0 0 0
811  
812 # Simple syllable counting heuristic
813 0 my $vowels = () = $word =~ /[aeiouy]/g;
814 0 ▶ $syllables += $vowels > 0 ? $vowels : 1;
Branch: 1 branch
BranchTrueFalseTotal
$vowels > 0 ? : 0 0 0
815 0 ▶ $syllables-- if $word =~ /e$/; # silent e
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /e$/ 0 0 0
816 }
817 0 ▶ $syllables = $word_count if $syllables < $word_count; # minimum 1 syllable per word
Branch: 1 branch
BranchTrueFalseTotal
if $syllables < $word_count 0 0 0
818  
819 # Flesch Reading Ease formula
820 0 my $avg_sentence_length = $word_count / $sentences;
821 0 my $avg_syllables_per_word = $syllables / $word_count;
822 0 my $flesch_score = 206.835 - (1.015 * $avg_sentence_length) - (84.6 * $avg_syllables_per_word);
823  
824 0 my $status = 'ok';
825 0 my $level;
826 my $notes;
827 0 my $resolution = '';
828  
829 0 ▶ if ($flesch_score >= 90) {
Branch: 6 branches
BranchTrueFalseTotal
if ($flesch_score >= 90) { } 0 0 0
elsif ($flesch_score >= 80) { } 0 0 0
elsif ($flesch_score >= 70) { } 0 0 0
elsif ($flesch_score >= 60) { } 0 0 0
elsif ($flesch_score >= 50) { } 0 0 0
elsif ($flesch_score >= 30) { } 0 0 0
830 0 $level = 'very easy';
831 } elsif ($flesch_score >= 80) {
832 0 $level = 'easy';
833 } elsif ($flesch_score >= 70) {
834 0 $level = 'fairly easy';
835 } elsif ($flesch_score >= 60) {
836 0 $level = 'standard';
837 } elsif ($flesch_score >= 50) {
838 0 $level = 'fairly difficult';
839 0 $status = 'warn';
840 0 $resolution = 'Consider simplifying: use shorter sentences (aim for 15-20 words), choose simpler words, break up long paragraphs, add bullet points or lists';
841 } elsif ($flesch_score >= 30) {
842 0 $level = 'difficult';
843 0 $status = 'warn';
844 0 $resolution = 'Improve readability: use much shorter sentences (10-15 words), replace complex words with simpler alternatives, add more paragraph breaks, use active voice';
845 } else {
846 0 $level = 'very difficult';
847 0 $status = 'warn';
848 0 $resolution = 'Significantly simplify content: break long sentences into multiple short ones, replace jargon with plain language, add explanations for technical terms, use more white space and formatting';
849 }
850  
851 0 $notes = sprintf('Flesch score: %.1f (%s) - %d words, %d sentences',
852 $flesch_score, $level, $word_count, $sentences);
853  
854 return {
855 0 name => 'Readability',
856 status => $status,
857 notes => $notes,
858 resolution => $resolution,
859 };
860 }
861  
862 =head1 AUTHOR
863  
864 Nigel Horne, C<< <njh at nigelhorne.com> >>
865  
866 =head1 SEE ALSO
867  
868 =over 4
869  
870 =item * Test coverage report: L<https://nigelhorne.github.io/SEO-Inspector/coverage/>
871  
872 =item * L<https://github.com/nigelhorne/SEO-Checker>
873  
874 =item * L<https://github.com/sethblack/python-seo-analyzer>
875  
876 =back
877  
878 =head1 REPOSITORY
879  
880 L<https://github.com/nigelhorne/SEO-Inspector>
881  
882 =head1 SUPPORT
883  
884 This module is provided as-is without any warranty.
885  
886 Please report any bugs or feature requests to C<bug-seo-inspector at rt.cpan.org>,
887 or through the web interface at
888 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SEO-Inspector>.
889 I will be notified, and then you'll
890 automatically be notified of progress on your bug as I make changes.
891  
892 You can find documentation for this module with the perldoc command.
893  
894 perldoc SEO::Inspector
895  
896 You can also look for information at:
897  
898 =over 4
899  
900 =item * MetaCPAN
901  
902 L<https://metacpan.org/dist/SEO-Inspector>
903  
904 =item * RT: CPAN's request tracker
905  
906 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=SEO-Inspector>
907  
908 =item * CPAN Testers' Matrix
909  
910 L<http://matrix.cpantesters.org/?dist=SEO-Inspector>
911  
912 =item * CPAN Testers Dependencies
913  
914 L<http://deps.cpantesters.org/?module=SEO::Inspector>
915  
916 =back
917  
918 =head1 LICENCE AND COPYRIGHT
919  
920 Copyright 2025 Nigel Horne.
921  
922 Usage is subject to licence terms.
923  
924 The licence terms of this software are as follows:
925  
926 =over 4
927  
928 =item * Personal single user, single computer use: GPL2
929  
930 =item * All other users (including Commercial, Charity, Educational, Government)
931 must apply in writing for a licence for use from Nigel Horne at the
932 above e-mail.
933  
934 =back
935  
936 =cut
937  
938 6 1;
939  
940 __END__
↑ Summary lib/SEO/Inspector/Plugin/MetaDescriptionCheck.pm »