blib/lib/MooX/Role/SEOTags.pm

filter: Statementstmt 96.6% 57 / 59 Branchbran 65.3% 17 / 26 Conditioncond 60.0% 18 / 30 MC/DCmcdc 40.0% 8 / 20 Subroutinesub 100% 25 / 25 Podpod 100% 19 / 19 totaltotal 80.4% 144 / 179 CC 24 SCAR 13.0
CC
24
Cov
80%
og_image_alt_tag
7.7
og_tags
7.5
twitter_card_tag
5.1
CRAP 3.7
↑ Summary
1  
2 package MooX::Role::SEOTags;
3  
4 =head1 NAME
5  
6 MooX::Role::SEOTags - A role for generating SEO meta tags (OpenGraph, Twitter, etc)
7  
8 =head1 SYNOPSIS
9  
10 package MyWebPage;
11 use Moo;
12 with 'MooX::Role::SEOTags';
13  
14 has og_title => (is => 'ro', required => 1);
15 has og_type => (is => 'ro', required => 1);
16 has og_description => (is => 'ro', required => 1);
17 has og_url => (is => 'ro', required => 1);
18 has og_image => (is => 'ro'); # optional
19  
20 # And later, in the code that builds the website
21  
22 my $page = MyWebPage->new(
23 og_title => "My Page Title",
24 og_type => "website",
25 og_description => 'This is a lovely website',
26 og_url => "https://example.com/my-page",
27 og_image => "https://example.com/image.jpg",
28 );
29  
30 # Print tags separately
31  
32 print $page->title_tag;
33 print $page->canonical_tag;
34 print $page->og_tags;
35  
36 # Or print all tags at once
37  
38 print $page->tags;
39  
40 =head1 DESCRIPTION
41  
42 This role provides methods to generate SEO meta tags for web pages, including OpenGraph and Twitter tags. It
43 requires the consuming class to implement the following attributes or methods:
44  
45 =over 4
46  
47 =item * og_title - The title of the page
48  
49 =item * og_type - The type of the page (e.g., "website", "article")
50  
51 =item * og_description - The description of the page
52  
53 =item * og_url - The canonical URL of the page
54  
55 =item * og_image - The URL of an image representing the content (optional)
56  
57 =back
58  
59 The role provides methods to generate individual tags as well as a method to
60 generate all tags at once.
61  
62 =head1 METHODS
63  
64 =head2 title_tag
65  
66 Returns the HTML title tag.
67  
68 =head2 author_tag
69  
70 Returns the optional HTML meta author tag.
71  
72 =head2 canonical_tag
73  
74 Returns the HTML canonical link tag.
75  
76 =head2 description_tag
77  
78 Returns the HTML meta description tag.
79  
80 =head2 core_tags
81  
82 Returns all core tags (title, author, canonical, and description) as a single string.
83  
84 =head2 og_title_tag
85  
86 Returns the OpenGraph title meta tag.
87  
88 =head2 og_type_tag
89  
90 Returns the OpenGraph type meta tag.
91  
92 =head2 og_description_tag
93  
94 Returns the OpenGraph description meta tag.
95  
96 =head2 og_url_tag
97  
98 Returns the OpenGraph URL meta tag.
99  
100 =head2 og_site_name_tag
101  
102 Returns the optional OpenGraph site name meta tag
103  
104 =head2 og_image_tag
105  
106 Returns the OpenGraph image meta tag if the og_image attribute is provided.
107  
108 =head2 og_image_alt_tag
109  
110 Returns the OpenGraph image alt meta tag is the og_image_alt attribute is
111 provided.
112  
113 =head2 og_tags
114  
115 Returns all OpenGraph meta tags as a single string.
116  
117 =head2 twitter_card_tag
118  
119 Returns the Twitter card meta tag.
120  
121 =head2 twitter_title_tag
122  
123 Returns the Twitter title meta tag.
124  
125 =head2 twitter_description_tag
126  
127 Returns the Twitter description meta tag.
128  
129 =head2 twitter_image_tag
130  
131 Returns the Twitter image meta tag if the og_image attribute is provided.
132  
133 =head2 twitter_tags
134  
135 Returns all Twitter meta tags as a single string.
136  
137 =head2 tags
138  
139 Returns all tags (title, canonical, and OpenGraph) as a single string.
140  
141 =head1 TAG GROUPS
142  
143 This role generates the following groups of HTML tags.
144  
145 =head2 Core Tags
146  
147 Returned by C<core_tags>.
148  
149 =over 4
150  
151 =item C<< E<lt>titleE<gt> >>
152  
153 Generated by C<title_tag>.
154  
155 =item C<< E<lt>meta name="author"E<gt> >>
156  
157 Generated by C<author_tag>. Optional.
158  
159 =item C<< E<lt>meta name="description"E<gt> >>
160  
161 Generated by C<description_tag>.
162  
163 =item C<< E<lt>link rel="canonical"E<gt> >>
164  
165 Generated by C<canonical_tag>.
166  
167 =back
168  
169 =head2 OpenGraph Tags
170  
171 Returned by C<og_tags>.
172  
173 =over 4
174  
175 =item C<< E<lt>meta property="og:title"E<gt> >>
176  
177 Generated by C<og_title_tag>.
178  
179 =item C<< E<lt>meta property="og:type"E<gt> >>
180  
181 Generated by C<og_type_tag>.
182  
183 =item C<< E<lt>meta property="og:description"E<gt> >>
184  
185 Generated by C<og_description_tag>.
186  
187 =item C<< E<lt>meta property="og:url"E<gt> >>
188  
189 Generated by C<og_url_tag>.
190  
191 =item C<< E<lt>meta property="og:image"E<gt> >>
192  
193 Generated by C<og_image_tag>. Optional.
194  
195 =item C<< E<lt>meta property="og:image:alt"E<gt> >>
196  
197 Generated by C<og_image_alt_tag>. Optional.
198  
199 =item C<< E<lt>meta property="og:site_name"E<gt> >>
200  
201 Generated by C<og_site_name_tag>. Optional.
202  
203 =back
204  
205 =head2 Twitter Tags
206  
207 Returned by C<twitter_tags>.
208  
209 =over 4
210  
211 =item C<< E<lt>meta name="twitter:card"E<gt> >>
212  
213 Generated by C<twitter_card_tag>.
214  
215 =item C<< E<lt>meta name="twitter:title"E<gt> >>
216  
217 Generated by C<twitter_title_tag>.
218  
219 =item C<< E<lt>meta name="twitter:description"E<gt> >>
220  
221 Generated by C<twitter_description_tag>.
222  
223 =item C<< E<lt>meta name="twitter:image"E<gt> >>
224  
225 Generated by C<twitter_image_tag>. Optional.
226  
227 =back
228  
229 =head2 All Tags
230  
231 Returned by C<tags>.
232  
233 This method returns all Core, OpenGraph and Twitter tags.
234  
235 =cut
236  
237 3 ▶use feature qw[signatures];
Subroutine BEGIN: called CC=1 SCAR=0.0
238 3 ▶no if $] <= 5.036, 'warnings', 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
239 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 3 ▶use HTML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
241  
242 3 our $VERSION = '1.2.2';
243  
244 3 requires qw[og_title og_type og_description og_url];
245  
246 has _html => (
247 is => 'lazy',
248 3 ▶ default => sub { HTML::Tiny->new(mode => 'html') },
Subroutine __ANON__: called CC=1 SCAR=0.0
249 3 );
250  
251 24 ▶sub _tag($self, $tag, @args) {
Subroutine _tag: called CC=1 SCAR=0.0
252 24 return $self->_html->$tag(@args);
253 }
254  
255 2 ▶sub title_tag($self) {
Subroutine title_tag: called CC=1 SCAR=0.0
Pod documented
256 2 return $self->_tag('title', {}, $self->og_title);
257 }
258  
259 2 ▶sub author_tag($self) {
Subroutine author_tag: called CC=2 SCAR=7.2
Pod documented
260 2 ▶ return '' unless $self->can('og_author');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("og_author") 0 2 2
261 2 return $self->_tag('meta', { name => 'author', content => $self->og_author });
262 }
263  
264 2 ▶sub canonical_tag($self) {
Subroutine canonical_tag: called CC=1 SCAR=0.0
Pod documented
265 2 return $self->_tag('link', { rel => 'canonical', href => $self->og_url });
266 }
267  
268 1 ▶sub description_tag($self) {
Subroutine description_tag: called CC=1 SCAR=0.0
Pod documented
269 1 return $self->_tag('meta', { name => "description", content => $self->og_description });
270 }
271  
272 1 ▶sub core_tags($self) {
Subroutine core_tags: called CC=2 SCAR=9.2
Pod documented
273 1 ▶ return join "\n", $self->title_tag,
Condition33%1/3
$self->author_tag || ()
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->author_tag || ()
$self->author_tag ()
Truth table33%1/3
A || B
A= $self->author_tagB= ()
ABresult
1X1
011
000
274 ($self->author_tag || ()),
275 $self->description_tag,
276 $self->canonical_tag;
277 }
278  
279 2 ▶sub og_title_tag($self) {
Subroutine og_title_tag: called CC=1 SCAR=0.0
Pod documented
280 2 return $self->_tag('meta', { property => "og:title", content => $self->og_title });
281 }
282  
283 2 ▶sub og_type_tag($self) {
Subroutine og_type_tag: called CC=1 SCAR=0.0
Pod documented
284 2 return $self->_tag('meta', { property => "og:type", content => $self->og_type });
285 }
286  
287 1 ▶sub og_description_tag($self) {
Subroutine og_description_tag: called CC=1 SCAR=0.0
Pod documented
288 1 return $self->_tag('meta', { property => "og:description", content => $self->og_description });
289 }
290  
291 1 ▶sub og_url_tag($self) {
Subroutine og_url_tag: called CC=1 SCAR=0.0
Pod documented
292 1 return $self->_tag('meta', { property => "og:url", content => $self->og_url });
293 }
294  
295 1 ▶sub og_site_name_tag($self) {
Subroutine og_site_name_tag: called CC=2 SCAR=9.2
Pod documented
296 1 ▶ return '' unless $self->can('og_site_name');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("og_site_name") 1 0 1
297 0 return $self->_tag('meta', { property => "og:site_name", content => $self->og_site_name });
298 }
299  
300 3 ▶sub og_image_tag($self) {
Subroutine og_image_tag: called CC=4 SCAR=13.9
Pod documented
301 3 ▶ return '' unless $self->can('og_image');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("og_image") 0 3 3
302 3 my $img = $self->og_image;
303 3 ▶ return '' unless defined $img && length $img;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $img and length $img 2 1 3
Condition100%3/3
defined $img and length $img
!ll&&!rl&&r
111
MC/DC100%2/2
defined $img and length $img
defined $img length $img
Truth table100%3/3
A and B
A= defined $imgB= length $img
ABresult
0X0
100
111
304 1 return $self->_tag('meta', { property => "og:image", content => $img });
305 }
306  
307 4 ▶sub og_image_alt_tag($self) {
Subroutine og_image_alt_tag: called CC=7 SCAR=20.4
Pod documented
308 4 ▶ return '' unless $self->can('og_image');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("og_image") 0 4 4
309 4 ▶ return '' unless $self->can('og_image_alt');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("og_image_alt") 0 4 4
310  
311 4 my $image = $self->og_image;
312 4 ▶ return '' unless defined $image && length $image;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $image and length $image 2 2 4
Condition100%3/3
defined $image and length $image
!ll&&!rl&&r
112
MC/DC100%2/2
defined $image and length $image
defined $image length $image
Truth table100%3/3
A and B
A= defined $imageB= length $image
ABresult
0X0
100
111
313  
314 2 my $alt = $self->og_image_alt;
315 2 ▶ return '' unless defined $alt && length $alt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alt and length $alt 0 2 2
Condition33%1/3
defined $alt and length $alt
!ll&&!rl&&r
002
MC/DC0%0/2
defined $alt and length $alt
defined $alt length $alt
Truth table33%1/3
A and B
A= defined $altB= length $alt
ABresult
0X0
100
111
316  
317 2 return $self->_tag(
318 'meta',
319 {
320 property => 'og:image:alt',
321 content => $alt,
322 },
323 );
324 }
325  
326 1 ▶sub og_tags($self) {
Subroutine og_tags: called CC=4 SCAR=20.1
Pod documented
327 1 ▶ return join "\n", $self->og_title_tag,
Condition33%3/9
$self->og_image_tag || ()
l!l&&r!l&&!r
100
$self->og_image_alt_tag || ()
l!l&&r!l&&!r
100
$self->og_site_name_tag || ()
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->og_image_tag || ()
$self->og_image_tag ()
MC/DC0%0/2
$self->og_image_alt_tag || ()
$self->og_image_alt_tag ()
MC/DC0%0/2
$self->og_site_name_tag || ()
$self->og_site_name_tag ()
Truth table33%1/3
A || B
A= $self->og_image_tagB= ()
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $self->og_image_alt_tagB= ()
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $self->og_site_name_tagB= ()
ABresult
1X1
011
000
328 $self->og_type_tag,
329 $self->og_description_tag,
330 $self->og_url_tag,
331 ($self->og_image_tag || ()),
332 ($self->og_image_alt_tag || ()),
333 ($self->og_site_name_tag || ());
334 }
335  
336 4 ▶sub twitter_card_tag($self) {
Subroutine twitter_card_tag: called CC=5 SCAR=16.3
Pod documented
337 4 my $card_type;
338  
339 4 ▶ if ($self->can('twitter_card_type')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('twitter_card_type')) { } 0 4 4
340 0 $card_type = $self->twitter_card_type;
341 }
342 else {
343 4 my $has_image = 0;
344  
345 4 ▶ if ($self->can('og_image')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("og_image")) 4 0 4
346 4 my $image = $self->og_image;
347 4 ▶ $has_image = defined $image && length $image;
Condition100%3/3
defined $image && length $image
!ll&&!rl&&r
112
MC/DC100%2/2
defined $image && length $image
defined $image length $image
Truth table100%3/3
A && B
A= defined $imageB= length $image
ABresult
0X0
100
111
348 }
349  
350 4 ▶ $card_type = $has_image
Branch: 1 branch
BranchTrueFalseTotal
$has_image ? : 2 2 4
351 ? 'summary_large_image'
352 : 'summary';
353 }
354  
355 4 return $self->_tag(
356 'meta',
357 {
358 name => 'twitter:card',
359 content => $card_type,
360 },
361 );
362 }
363  
364 2 ▶sub twitter_title_tag($self) {
Subroutine twitter_title_tag: called CC=1 SCAR=0.0
Pod documented
365 2 return $self->_tag('meta', { name => "twitter:title", content => $self->og_title });
366 }
367  
368 1 ▶sub twitter_description_tag($self) {
Subroutine twitter_description_tag: called CC=1 SCAR=0.0
Pod documented
369 1 return $self->_tag('meta', { name => "twitter:description", content => $self->og_description });
370 }
371  
372 3 ▶sub twitter_image_tag($self) {
Subroutine twitter_image_tag: called CC=4 SCAR=13.9
Pod documented
373 3 ▶ return '' unless $self->can('og_image');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("og_image") 0 3 3
374  
375 3 my $image = $self->og_image;
376 3 ▶ return '' unless defined $image && length $image;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $image and length $image 2 1 3
Condition100%3/3
defined $image and length $image
!ll&&!rl&&r
111
MC/DC100%2/2
defined $image and length $image
defined $image length $image
Truth table100%3/3
A and B
A= defined $imageB= length $image
ABresult
0X0
100
111
377  
378 1 return $self->_tag(
379 'meta',
380 {
381 name => 'twitter:image',
382 content => $image,
383 },
384 );
385 }
386  
387 1 ▶sub twitter_tags($self) {
Subroutine twitter_tags: called CC=2 SCAR=9.2
Pod documented
388 1 ▶ return join "\n", $self->twitter_card_tag,
Condition33%1/3
$self->twitter_image_tag || ()
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->twitter_image_tag || ()
$self->twitter_image_tag ()
Truth table33%1/3
A || B
A= $self->twitter_image_tagB= ()
ABresult
1X1
011
000
389 $self->twitter_title_tag,
390 $self->twitter_description_tag,
391 ($self->twitter_image_tag || ());
392 }
393  
394 1 ▶sub tags($self) {
Subroutine tags: called CC=1 SCAR=0.0
Pod documented
395 1 return join "\n", $self->core_tags,
396 $self->og_tags,
397 $self->twitter_tags;
398 }
399  
400 3 1;
401  
402 =head1 AUTHOR
403  
404 Dave Cross E<lt>dave@perlhacks.comE<gt>
405  
406 =head1 COPYRIGHT
407  
408 Copyright (c) 2025-26 Magnum Solutions Ltd. This program is free software; you can
409 redistribute it and/or modify it under the same terms as Perl itself.
410  
411 =cut
↑ Summary