| 1 | package Mojolicious::Plugin::Badge; | ||
| 2 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 2 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Image::Magick; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Mojo::ByteStream; | ||
| 7 | use Mojo::File qw(path); | ||
| 8 | use Mojo::URL; | ||
| 9 | use Mojo::UserAgent; | ||
| 10 | use Mojo::Util qw(b64_encode); | ||
| 11 | use Mojolicious::Types; | ||
| 12 | |||
| 13 | our $VERSION = '1.12'; | ||
| 14 | |||
| 15 | use constant DEBUG => $ENV{BADGE_PLUGIN_DEBUG} || 0; | ||
| 16 | |||
| 17 | # Shields colors | ||
| 18 | my %SHIELDS_COLORS = ( | ||
| 19 | brightgreen => '#4c1', | ||
| 20 | green => '#97ca00', | ||
| 21 | yellow => '#dfb317', | ||
| 22 | yellowgreen => '#a4a61d', | ||
| 23 | orange => '#fe7d37', | ||
| 24 | red => '#e05d44', | ||
| 25 | blue => '#007ec6', | ||
| 26 | grey => '#555', | ||
| 27 | lightgrey => '#9f9f9f', | ||
| 28 | |||
| 29 | # Alias | ||
| 30 | gray => '#555', | ||
| 31 | lightgray => '#9f9f9f', | ||
| 32 | critical => '#e05d44', | ||
| 33 | important => '#fe7d37', | ||
| 34 | success => '#4c1', | ||
| 35 | informational => '#007ec6', | ||
| 36 | inactive => '#9f9f9f', | ||
| 37 | ); | ||
| 38 | |||
| 39 | my %NAMED_COLORS = ( | ||
| 40 | aliceblue => '#f0f8ff', | ||
| 41 | antiquewhite => '#faebd7', | ||
| 42 | aqua => '#00ffff', | ||
| 43 | aquamarine => '#7fffd4', | ||
| 44 | azure => '#f0ffff', | ||
| 45 | beige => '#f5f5dc', | ||
| 46 | bisque => '#ffe4c4', | ||
| 47 | black => '#000000', | ||
| 48 | blanchedalmond => '#ffebcd', | ||
| 49 | blue => '#0000ff', | ||
| 50 | blueviolet => '#8a2be2', | ||
| 51 | brown => '#a52a2a', | ||
| 52 | burlywood => '#deb887', | ||
| 53 | cadetblue => '#5f9ea0', | ||
| 54 | chartreuse => '#7fff00', | ||
| 55 | chocolate => '#d2691e', | ||
| 56 | coral => '#ff7f50', | ||
| 57 | cornflowerblue => '#6495ed', | ||
| 58 | cornsilk => '#fff8dc', | ||
| 59 | crimson => '#dc143c', | ||
| 60 | cyan => '#00ffff', | ||
| 61 | darkblue => '#00008b', | ||
| 62 | darkcyan => '#008b8b', | ||
| 63 | darkgoldenrod => '#b886b', | ||
| 64 | darkgray => '#a9a9a9', | ||
| 65 | darkgreen => '#006400', | ||
| 66 | darkgrey => '#a9a9a9', | ||
| 67 | darkkhaki => '#bdb76b', | ||
| 68 | darkmagenta => '#8b008b', | ||
| 69 | darkolivegreen => '#556b2f', | ||
| 70 | darkorange => '#ff8c00', | ||
| 71 | darkorchid => '#9932cc', | ||
| 72 | darkred => '#8b0000', | ||
| 73 | darksalmon => '#e9967a', | ||
| 74 | darkseagreen => '#8fbc8f', | ||
| 75 | darkslateblue => '#483d8b', | ||
| 76 | darkslategray => '#2f4f4f', | ||
| 77 | darkslategrey => '#2f4f4f', | ||
| 78 | darkturquoise => '#00ced1', | ||
| 79 | darkviolet => '#9400d3', | ||
| 80 | deeppink => '#ff1493', | ||
| 81 | deepskyblue => '#00bfff', | ||
| 82 | dimgray => '#696969', | ||
| 83 | dimgrey => '#696969', | ||
| 84 | dodgerblue => '#1e90ff', | ||
| 85 | firebrick => '#b22222', | ||
| 86 | floralwhite => '#fffaf0', | ||
| 87 | forestgreen => '#228b22', | ||
| 88 | fuchsia => '#ff00ff', | ||
| 89 | gainsboro => '#dcdcdc', | ||
| 90 | ghostwhite => '#f8f8ff', | ||
| 91 | gold => '#ffd700', | ||
| 92 | goldenrod => '#daa520', | ||
| 93 | gray => '#808080', | ||
| 94 | green => '#008000', | ||
| 95 | greenyellow => '#adff2f', | ||
| 96 | grey => '#808080', | ||
| 97 | honeydew => '#f0fff0', | ||
| 98 | hotpink => '#ff69b4', | ||
| 99 | indianred => '#cd5c5c', | ||
| 100 | indigo => '#4b0082', | ||
| 101 | ivory => '#fffff0', | ||
| 102 | khaki => '#f0e68c', | ||
| 103 | lavender => '#e6e6fa', | ||
| 104 | lavenderblush => '#fff0f5', | ||
| 105 | lawngreen => '#7cfc00', | ||
| 106 | lemonchiffon => '#fffacd', | ||
| 107 | lightblue => '#add8e6', | ||
| 108 | lightcoral => '#f08080', | ||
| 109 | lightcyan => '#e0ffff', | ||
| 110 | lightgoldenrodyellow => '#fafad2', | ||
| 111 | lightgray => '#d3d3d3', | ||
| 112 | lightgreen => '#90ee90', | ||
| 113 | lightgrey => '#d3d3d3', | ||
| 114 | lightpink => '#ffb6c1', | ||
| 115 | lightsalmon => '#ffa07a', | ||
| 116 | lightseagreen => '#20b2aa', | ||
| 117 | lightskyblue => '#87cefa', | ||
| 118 | lightslategray => '#778899', | ||
| 119 | lightslategrey => '#778899', | ||
| 120 | lightsteelblue => '#b0c4de', | ||
| 121 | lightyellow => '#ffffe0', | ||
| 122 | lime => '#00ff00', | ||
| 123 | limegreen => '#32cd32', | ||
| 124 | linen => '#faf0e6', | ||
| 125 | magenta => '#ff00ff', | ||
| 126 | maroon => '#800000', | ||
| 127 | mediumaquamarine => '#66cdaa', | ||
| 128 | mediumblue => '#0000cd', | ||
| 129 | mediumorchid => '#ba55d3', | ||
| 130 | mediumpurple => '#9370db', | ||
| 131 | mediumseagreen => '#3cb371', | ||
| 132 | mediumslateblue => '#7b68ee', | ||
| 133 | mediumspringgreen => '#00fa9a', | ||
| 134 | mediumturquoise => '#48d1cc', | ||
| 135 | mediumvioletred => '#c71585', | ||
| 136 | midnightblue => '#191970', | ||
| 137 | mintcream => '#f5fffa', | ||
| 138 | mistyrose => '#ffe4e1', | ||
| 139 | moccasin => '#ffe4b5', | ||
| 140 | navajowhite => '#ffdead', | ||
| 141 | navy => '#000080', | ||
| 142 | oldlace => '#fdf5e6', | ||
| 143 | olive => '#808000', | ||
| 144 | olivedrab => '#6b8e23', | ||
| 145 | orange => '#ffa500', | ||
| 146 | orangered => '#ff4500', | ||
| 147 | orchid => '#da70d6', | ||
| 148 | palegoldenrod => '#eee8aa', | ||
| 149 | palegreen => '#98fb98', | ||
| 150 | paleturquoise => '#afeeee', | ||
| 151 | palevioletred => '#db7093', | ||
| 152 | papayawhip => '#ffefd5', | ||
| 153 | peachpuff => '#ffdab9', | ||
| 154 | peru => '#cd853f', | ||
| 155 | pink => '#ffc0cb', | ||
| 156 | plum => '#dda0dd', | ||
| 157 | powderblue => '#b0e0e6', | ||
| 158 | purple => '#800080', | ||
| 159 | rebeccapurple => '#663399', | ||
| 160 | red => '#ff0000', | ||
| 161 | rosybrown => '#bc8f8f', | ||
| 162 | royalblue => '#4169e1', | ||
| 163 | saddlebrown => '#8b4513', | ||
| 164 | salmon => '#fa8072', | ||
| 165 | sandybrown => '#f4a460', | ||
| 166 | seagreen => '#2e8b57', | ||
| 167 | seashell => '#fff5ee', | ||
| 168 | sienna => '#a0522d', | ||
| 169 | silver => '#c0c0c0', | ||
| 170 | skyblue => '#87ceeb', | ||
| 171 | slateblue => '#6a5acd', | ||
| 172 | slategray => '#708090', | ||
| 173 | slategrey => '#708090', | ||
| 174 | snow => '#fffafa', | ||
| 175 | springgreen => '#00ff7f', | ||
| 176 | steelblue => '#4682b4', | ||
| 177 | tan => '#d2b48c', | ||
| 178 | teal => '#008080', | ||
| 179 | thistle => '#d8bfd8', | ||
| 180 | tomato => '#ff6347', | ||
| 181 | turquoise => '#40e0d0', | ||
| 182 | violet => '#ee82ee', | ||
| 183 | wheat => '#f5deb3', | ||
| 184 | white => '#ffffff', | ||
| 185 | whitesmoke => '#f5f5f5', | ||
| 186 | yellow => '#ffff00', | ||
| 187 | yellowgreen => '#9acd32', | ||
| 188 | ); | ||
| 189 | |||
| 190 | sub register { | ||
| 191 | |||
| 192 | my ($self, $app, $config) = @_; | ||
| 193 | |||
| 194 | # Config | ||
| 195 | my $prefix = $config->{route} // $app->routes->any('/badge'); | ||
| 196 | $prefix->to(return_to => $config->{return_to} // '/'); | ||
| 197 | |||
| 198 | # Templates | ||
| 199 | my $resources = path(__FILE__)->sibling('Badge', 'resources'); | ||
| 200 | push @{$app->renderer->paths}, $resources->child('templates')->to_string; | ||
| 201 | |||
| 202 | # Routes | ||
| 203 | if (!$config->{disable_api}) { | ||
| 204 | $prefix->get('/#content' => \&_badge_api)->to('content' => undef)->name('badge'); | ||
| 205 | } | ||
| 206 | |||
| 207 | # Helpers | ||
| 208 | $app->helper('badge', sub { Mojo::ByteStream->new(_badge(@_)) }); | ||
| 209 | |||
| 210 | } | ||
| 211 | |||
| 212 | sub _badge { | ||
| 213 | |||
| 214 | my ($c, %options) = @_; | ||
| 215 | |||
| 216 | my %badge_options = _build_options(%options); | ||
| 217 | |||
| 218 | DEBUG and $c->log->debug('[Badge] User Config', $c->app->dumper(\%options)); | ||
| 219 | DEBUG and $c->log->debug('[Badge] Badge Config', $c->app->dumper(\%badge_options)); | ||
| 220 | |||
| 221 | my $svg = $c->render_to_string('badge', format => 'svg', %badge_options); | ||
| 222 | |||
| 223 | if ($badge_options{badge_format} eq 'png') { | ||
| 224 | |||
| 225 | my $image = Image::Magick->new(magick => 'svg'); | ||
| 226 | $image->BlobToImage($svg); | ||
| 227 | |||
| 228 | return $image->ImageToBlob(magick => 'png'); | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | # Minify SVG | ||
| 233 | $svg =~ s/^(\s+)//mg; | ||
| 234 | $svg =~ s/\n//mg; | ||
| 235 | |||
| 236 | return $svg; | ||
| 237 | |||
| 238 | } | ||
| 239 | |||
| 240 | sub _build_options { | ||
| 241 | |||
| 242 | my (%options) = @_; | ||
| 243 | |||
| 244 | my $embed_logo = $options{embed_logo}; | ||
| 245 | |||
| 246 | my $id_suffix = $options{id_suffix}; | ||
| 247 | |||
| 248 | my $logo = $options{logo}; | ||
| 249 | my $color = _decode_color($options{color} || 'informational'); | ||
| 250 | my $link = $options{link}; | ||
| 251 | my $title = $options{title}; | ||
| 252 | my $style = $options{style} || 'flat'; | ||
| 253 | my $badge_format = $options{badge_format} || 'svg'; | ||
| 254 | |||
| 255 | if ($style !~ /^(flat|flat-square|plastic|for-the-badge)$/) { | ||
| 256 | Carp::croak 'Unknown badge style'; | ||
| 257 | } | ||
| 258 | |||
| 259 | if ($badge_format !~ /^(png|svg)$/) { | ||
| 260 | Carp::croak 'Unknown badge format'; | ||
| 261 | } | ||
| 262 | |||
| 263 | my $label = $options{label} || Carp::croak 'Missing label'; | ||
| 264 | my $label_color = _decode_color($options{label_color} || 'gray'); | ||
| 265 | my $label_link = $options{label_link}; | ||
| 266 | my $label_text_color = _decode_color($options{label_text_color} || 'white'); | ||
| 267 | my $label_title = $options{label_title}; | ||
| 268 | |||
| 269 | my $message = $options{message}; | ||
| 270 | my $message_link = $options{message_link}; | ||
| 271 | my $message_text_color = _decode_color($options{message_text_color} || 'white'); | ||
| 272 | my $message_title = $options{message_title}; | ||
| 273 | |||
| 274 | if (($label_link || $message_link) && $link) { | ||
| 275 | Carp::croak '"link" may not bet set with "label_link" or "message_link"'; | ||
| 276 | } | ||
| 277 | |||
| 278 | if ($style eq 'for-the-badge') { | ||
| 279 | $label = uc($label); | ||
| 280 | $message = uc($message) if ($message); | ||
| 281 | } | ||
| 282 | |||
| 283 | my $label_text_width = _get_text_width($label); | ||
| 284 | my $message_text_width = 0; | ||
| 285 | |||
| 286 | if ($message) { | ||
| 287 | if ($style eq 'for-the-badge') { | ||
| 288 | $message_text_width = _get_text_width($message, font => 'DejaVu-Sans-Bold'); | ||
| 289 | } | ||
| 290 | else { | ||
| 291 | $message_text_width = _get_text_width($message); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | # Embed image if is a file | ||
| 296 | $options{embed_logo} = 1 if ($logo && -e $logo); | ||
| 297 | |||
| 298 | # Embed image if badge format is "png" | ||
| 299 | $options{embed_logo} = 1 if ($logo && $badge_format eq 'png'); | ||
| 300 | |||
| 301 | $logo = _embed_image($logo) if ($options{logo} && $options{embed_logo}); | ||
| 302 | |||
| 303 | # FIX label color | ||
| 304 | $label_text_color = '#333' if ($label_color =~ /white/i || $label_color =~ /#FFFFFF/i); | ||
| 305 | |||
| 306 | my $aria_label = $title; | ||
| 307 | $aria_label = "$label_title: $message_title" if ($label_title && $message_title); | ||
| 308 | $aria_label = "$label: $message" if ($label && $message); | ||
| 309 | |||
| 310 | $link = Mojo::URL->new($link) if $link; | ||
| 311 | $label_link = Mojo::URL->new($label_link) if $label_link; | ||
| 312 | $message_link = Mojo::URL->new($message_link) if $message_link; | ||
| 313 | |||
| 314 | return ( | ||
| 315 | aria_label => $aria_label, | ||
| 316 | badge_format => $badge_format, | ||
| 317 | color => $color, | ||
| 318 | embed_logo => $embed_logo, | ||
| 319 | id_suffix => $id_suffix, | ||
| 320 | label => $label, | ||
| 321 | label_color => $label_color, | ||
| 322 | label_link => $label_link, | ||
| 323 | label_text_color => $label_text_color, | ||
| 324 | label_text_width => $label_text_width, | ||
| 325 | label_title => $label_title, | ||
| 326 | link => $link, | ||
| 327 | logo => $logo, | ||
| 328 | message => $message, | ||
| 329 | message_link => $message_link, | ||
| 330 | message_text_color => $message_text_color, | ||
| 331 | message_text_width => $message_text_width, | ||
| 332 | message_title => $message_title, | ||
| 333 | style => $style, | ||
| 334 | title => $title, | ||
| 335 | ); | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | sub _decode_color { | ||
| 340 | |||
| 341 | my $color = shift; | ||
| 342 | |||
| 343 | return unless $color; | ||
| 344 | |||
| 345 | return $SHIELDS_COLORS{$color} if (defined $SHIELDS_COLORS{$color}); | ||
| 346 | return $NAMED_COLORS{$color} if (defined $NAMED_COLORS{$color}); | ||
| 347 | return sprintf('#%s', lc($color)) if ($color =~ /^(?:[0-9a-fA-F]{3}){1,2}$/); | ||
| 348 | |||
| 349 | } | ||
| 350 | |||
| 351 | sub _badge_api { | ||
| 352 | |||
| 353 | my $c = shift; | ||
| 354 | |||
| 355 | # Shields.io compatibility | ||
| 356 | my $badge_content = $c->param('content') || $c->param('badgeContent'); | ||
| 357 | my $link = $c->param('link'); | ||
| 358 | my $label = $c->param('label'); | ||
| 359 | my $label_color = $c->param('labelColor'); | ||
| 360 | my $color = $c->param('color'); | ||
| 361 | my $logo = $c->param('logo'); | ||
| 362 | my $style = $c->param('style') || 'flat'; | ||
| 363 | |||
| 364 | my $log = $c->app->log->context('[Badge API]'); | ||
| 365 | my %options = (); | ||
| 366 | |||
| 367 | DEBUG and $log->debug("Content: $badge_content"); | ||
| 368 | |||
| 369 | if ($badge_content) { | ||
| 370 | |||
| 371 | $badge_content =~ s/\-{2}/\0/g; | ||
| 372 | $badge_content =~ s/\_{2}/\t/g; | ||
| 373 | $badge_content =~ s/_/ /g; | ||
| 374 | $badge_content =~ s/\t/_/g; | ||
| 375 | |||
| 376 | my ($_label, $_message, $_color) = split '-', $badge_content; | ||
| 377 | |||
| 378 | $_label =~ s/\0/-/g; | ||
| 379 | $_message =~ s/\0/-/g; | ||
| 380 | |||
| 381 | DEBUG and $log->debug("Label => $_label - Message => $_message - Color => $_color"); | ||
| 382 | |||
| 383 | if ($_color =~ /\.(png|svg)$/) { | ||
| 384 | $options{badge_format} = $1; | ||
| 385 | $_color =~ s/(\.(png|svg))$//; | ||
| 386 | } | ||
| 387 | |||
| 388 | $options{label} = $_label; | ||
| 389 | $options{message} = $_message; | ||
| 390 | $options{color} = $_color; | ||
| 391 | |||
| 392 | $options{title} = "$_label: $_message"; | ||
| 393 | |||
| 394 | if (!$_color) { | ||
| 395 | |||
| 396 | if ($options{message} =~ /\.(png|svg)$/) { | ||
| 397 | $options{badge_format} = $1; | ||
| 398 | $options{message} =~ s/(\.(png|svg))$//; | ||
| 399 | } | ||
| 400 | |||
| 401 | $options{label_color} = $options{message}; | ||
| 402 | $options{title} = $options{label}; | ||
| 403 | |||
| 404 | delete $options{message}; | ||
| 405 | delete $options{color}; | ||
| 406 | |||
| 407 | } | ||
| 408 | |||
| 409 | } | ||
| 410 | |||
| 411 | |||
| 412 | $options{badge_format} //= 'svg'; | ||
| 413 | |||
| 414 | $options{link} = $link if ($link); | ||
| 415 | $options{label} = $label if ($label); | ||
| 416 | $options{label_color} = $label_color if ($label_color); | ||
| 417 | $options{color} = $color if ($color); | ||
| 418 | $options{style} = $style if ($style); | ||
| 419 | |||
| 420 | my $status = 200; | ||
| 421 | |||
| 422 | if (!$options{label}) { | ||
| 423 | $options{label} = 404; | ||
| 424 | $options{message} = 'badge not found'; | ||
| 425 | $options{color} = 'red'; | ||
| 426 | $status = 404; | ||
| 427 | } | ||
| 428 | |||
| 429 | my $badge = _badge($c, %options); | ||
| 430 | |||
| 431 | if ($options{badge_format} eq 'png') { | ||
| 432 | return $c->render(data => $badge, format => 'png', status => $status); | ||
| 433 | } | ||
| 434 | |||
| 435 | return $c->render(text => $badge, format => 'svg', status => $status); | ||
| 436 | |||
| 437 | } | ||
| 438 | |||
| 439 | sub _embed_image { | ||
| 440 | |||
| 441 | my $image = shift; | ||
| 442 | my $ua = Mojo::UserAgent->new; | ||
| 443 | my $log = Mojo::Log->new->context('[Badge]'); | ||
| 444 | |||
| 445 | my $content = undef; | ||
| 446 | my $content_type = undef; | ||
| 447 | |||
| 448 | if ($image =~ /^http/) { | ||
| 449 | |||
| 450 | my $url = (ref($image) ne 'Mojo::URL') ? Mojo::URL->new($image) : $image; | ||
| 451 | |||
| 452 | $log->info("Embedding image from URL: $url"); | ||
| 453 | |||
| 454 | my $tx = $ua->get($url); | ||
| 455 | |||
| 456 | if (my $err = $tx->error) { | ||
| 457 | $log->error('Embed image error:', $err->{message}); | ||
| 458 | return; | ||
| 459 | } | ||
| 460 | |||
| 461 | my $res = $tx->result; | ||
| 462 | |||
| 463 | $content_type = $res->headers->content_type; | ||
| 464 | |||
| 465 | if ($content_type !~ /^image/) { | ||
| 466 | $log->error(sprintf(q{URL doesn't contain an image (%s)}, $content_type)); | ||
| 467 | return; | ||
| 468 | } | ||
| 469 | |||
| 470 | $content = $res->body; | ||
| 471 | |||
| 472 | } | ||
| 473 | |||
| 474 | if (-e $image) { | ||
| 475 | |||
| 476 | my $file = (ref($image) ne 'Mojo::File') ? Mojo::File->new($image) : $image; | ||
| 477 | my $types = Mojolicious::Types->new; | ||
| 478 | |||
| 479 | $content_type = $types->file_type($file); | ||
| 480 | |||
| 481 | if ($content_type !~ /^image/) { | ||
| 482 | $log->error(sprintf('File is not an image (%s)', $content_type)); | ||
| 483 | return; | ||
| 484 | } | ||
| 485 | |||
| 486 | $content = $file->slurp; | ||
| 487 | |||
| 488 | } | ||
| 489 | |||
| 490 | return sprintf('data:%s;base64,%s', $content_type, b64_encode($content)); | ||
| 491 | |||
| 492 | } | ||
| 493 | |||
| 494 | sub _get_text_width { | ||
| 495 | |||
| 496 | my ($text, %properties) = @_; | ||
| 497 | |||
| 498 | my $image = Image::Magick->new; | ||
| 499 | |||
| 500 | $properties{pointsize} ||= 110; | ||
| 501 | $properties{text} ||= $text; | ||
| 502 | $properties{gravity} ||= 'northwest'; | ||
| 503 | $properties{font} ||= 'DejaVu-Sans'; | ||
| 504 | |||
| 505 | $image->Set(size => '1x1'); | ||
| 506 | $image->ReadImage('xc:none'); | ||
| 507 | |||
| 508 | my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) | ||
| 509 | = $image->QueryMultilineFontMetrics(%properties); | ||
| 510 | |||
| 511 | return sprintf('%.0f', ($width / 10.0)); | ||
| 512 | |||
| 513 | } | ||
| 514 | |||
| 515 | 1; | ||
| 516 | |||
| 517 | =encoding utf8 | ||
| 518 | |||
| 519 | =head1 NAME | ||
| 520 | |||
| 521 | Mojolicious::Plugin::Badge - Badge Plugin for Mojolicious | ||
| 522 | |||
| 523 | =head1 SYNOPSIS | ||
| 524 | |||
| 525 | # Mojolicious | ||
| 526 | $self->plugin('Badge'); | ||
| 527 | |||
| 528 | # Mojolicious::Lite | ||
| 529 | plugin 'Badge'; | ||
| 530 | |||
| 531 | get '/my-cool-badge' => sub ($c) { | ||
| 532 | |||
| 533 | my $badge = $c->app->badge( | ||
| 534 | label => 'Hello', | ||
| 535 | message => 'Mojo!', | ||
| 536 | color => 'orange' | ||
| 537 | logo => 'https://docs.mojolicious.org/mojo/logo.png' | ||
| 538 | badge_format => 'png', | ||
| 539 | ); | ||
| 540 | |||
| 541 | $c->render(data => $badge, format => 'png'); | ||
| 542 | |||
| 543 | }; | ||
| 544 | |||
| 545 | |||
| 546 | =head1 DESCRIPTION | ||
| 547 | |||
| 548 | L<Mojolicious::Plugin::Badge> is a L<Mojolicious> plugin that generate "Shields.io" | ||
| 549 | like badge from L</badge> helper or via API URL (e.g. C</badge/Hello-Mojo!-orange>). | ||
| 550 | |||
| 551 | =begin html | ||
| 552 | |||
| 553 | <p> | ||
| 554 | <img alt="Hello Mojo!" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/hello-mojo.png?raw=true"> | ||
| 555 | </p> | ||
| 556 | |||
| 557 | =end html | ||
| 558 | |||
| 559 | |||
| 560 | =head1 OPTIONS | ||
| 561 | |||
| 562 | L<Mojolicious::Plugin::Badge> supports the following options. | ||
| 563 | |||
| 564 | =head2 disable_api | ||
| 565 | |||
| 566 | # Mojolicious::Lite | ||
| 567 | plugin 'Badge' => {disable_api => 1}; | ||
| 568 | |||
| 569 | Disable the L</API URL>. | ||
| 570 | |||
| 571 | =head2 route | ||
| 572 | |||
| 573 | # Mojolicious::Lite | ||
| 574 | plugin 'Badge' => {route => app->routes->any('/stuff')}; | ||
| 575 | |||
| 576 | L<Mojolicious::Routes::Route> object to attach the badge API URL, defaults to | ||
| 577 | generating a new one with the prefix C</badge>. | ||
| 578 | |||
| 579 | |||
| 580 | =head1 METHODS | ||
| 581 | |||
| 582 | L<Mojolicious::Plugin::Badge> inherits all methods from | ||
| 583 | L<Mojolicious::Plugin> and implements the following new ones. | ||
| 584 | |||
| 585 | =head2 badge | ||
| 586 | |||
| 587 | $plugin->badge( %options ); | ||
| 588 | |||
| 589 | Build and render a badge in SVG (or PNG) format. | ||
| 590 | |||
| 591 | =head3 %options | ||
| 592 | |||
| 593 | =over | ||
| 594 | |||
| 595 | =item * C<badge_format>, Badge image format, C<svg> (default) or C<png>. | ||
| 596 | |||
| 597 | =item * C<color>, Message color (see L</COLORS>) | ||
| 598 | |||
| 599 | =item * C<embed_logo>, Includes logo in badge | ||
| 600 | |||
| 601 | =item * C<id_suffix>, The suffix of the id attributes used in the SVG's elements. | ||
| 602 | Use to prevent duplicate ids if several badges are embedded on the same page. | ||
| 603 | |||
| 604 | =item * C<label>, The text that should appear on the left-hand-side of the badge | ||
| 605 | |||
| 606 | =item * C<label_color>, Label color (see L</COLORS>) | ||
| 607 | |||
| 608 | =item * C<label_link>, The URL that should be redirected to when the right-hand | ||
| 609 | text is selected. | ||
| 610 | |||
| 611 | =item * C<label_text_color>, Label text color (see L</COLORS>) | ||
| 612 | |||
| 613 | =item * C<label_title>, The title attribute to associate with the left part of the | ||
| 614 | badge. | ||
| 615 | |||
| 616 | =item * C<link>, Link for the whole badge (works only for SVG badge) | ||
| 617 | |||
| 618 | =item * C<logo>, A file, URL or data (e.g. "data:image/svg+xml;utf8,<svg...") | ||
| 619 | representing a logo that will be displayed inside the badge. | ||
| 620 | |||
| 621 | =item * C<message> The text that should appear on the right-hand-side of the badge | ||
| 622 | |||
| 623 | =item * C<message_link>, The URL that should be redirected to when the right-hand | ||
| 624 | text is selected. | ||
| 625 | |||
| 626 | =item * C<message_text_color>, Message text color (see L</COLORS>) | ||
| 627 | |||
| 628 | =item * C<message_title>, The title attribute to associate with the right part of the | ||
| 629 | badge. | ||
| 630 | |||
| 631 | =item * C<style>, Badge style (see L</STYLES>) | ||
| 632 | |||
| 633 | =item * C<title>, The title attribute to associate with the entire badge. | ||
| 634 | See L<https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title>. | ||
| 635 | |||
| 636 | =back | ||
| 637 | |||
| 638 | C<(label|message)_title>, C<(label|message)_link>, C<title> and C<link> options | ||
| 639 | works only for SVG badge. | ||
| 640 | |||
| 641 | =head2 register | ||
| 642 | |||
| 643 | $plugin->register(Mojolicious->new); | ||
| 644 | |||
| 645 | Register plugin in L<Mojolicious> application. | ||
| 646 | |||
| 647 | =head1 API URL | ||
| 648 | |||
| 649 | The default base URL is C</badge>. You can change the base URL via C<route> option (see L</OPTIONS>). | ||
| 650 | |||
| 651 | A badge require a single path parameter with C<label>, C<message> and C<color> separated by dash (C<->). | ||
| 652 | |||
| 653 | Example: | ||
| 654 | |||
| 655 | /badge/label-message-color | ||
| 656 | |||
| 657 | =head2 Escape | ||
| 658 | |||
| 659 | =over | ||
| 660 | |||
| 661 | =item * Underscore C<_> or C<%20> are converted to space C< > | ||
| 662 | |||
| 663 | =item * Double underscore C<__> is converted to underscore C<_> | ||
| 664 | |||
| 665 | =item * Double dash C<--> is converted to dash C<-> | ||
| 666 | |||
| 667 | =back | ||
| 668 | |||
| 669 | Examples: | ||
| 670 | |||
| 671 | /badge/Hello-Mojo!-green | ||
| 672 | |||
| 673 | /badge/Mojolicious--Plugin--Badge-1.0.0-green | ||
| 674 | |||
| 675 | |||
| 676 | =head2 Query parametrers | ||
| 677 | |||
| 678 | =over | ||
| 679 | |||
| 680 | =item * C<style>, If not specified, the default style for this badge is C<flat> (see L</STYLES>) | ||
| 681 | |||
| 682 | =item * C<label>, Override the default left-hand-side text | ||
| 683 | |||
| 684 | =item * C<labelColor>, Background color of the left part | ||
| 685 | |||
| 686 | =item * C<color>, Background color of the right part | ||
| 687 | |||
| 688 | =item * C<link>, Specify what clicking on the left/right of a badge should do. | ||
| 689 | Note that this only works when integrating your badge in an C<E<lt>objectE<gt>> HTML | ||
| 690 | tag, but not an C<E<lt>imgE<gt>> tag or a markup language. | ||
| 691 | |||
| 692 | =back | ||
| 693 | |||
| 694 | =head2 Image format | ||
| 695 | |||
| 696 | Badge API supports C<svg> (default) and C<png> image formats. | ||
| 697 | |||
| 698 | Examples: | ||
| 699 | |||
| 700 | /badge/Hello-Mojo!-green | ||
| 701 | /badge/Hello-Mojo!-green.svg | ||
| 702 | |||
| 703 | /badge/Hello-Mojo!-green.png | ||
| 704 | |||
| 705 | |||
| 706 | =head1 COLORS | ||
| 707 | |||
| 708 | The L<badge> method support named and HEX colors: | ||
| 709 | |||
| 710 | # Named color | ||
| 711 | $plugin->badge( color => 'orange', label => 'Status', message => 'Warning' ); | ||
| 712 | |||
| 713 | # HEX color | ||
| 714 | $plugin->badge( color => 'fe7d37', label => 'Status', message => 'Warning' ); | ||
| 715 | |||
| 716 | |||
| 717 | Shields colors: | ||
| 718 | |||
| 719 | =over | ||
| 720 | |||
| 721 | =item * C<brightgreen> | ||
| 722 | |||
| 723 | =item * C<green> | ||
| 724 | |||
| 725 | =item * C<yellow> | ||
| 726 | |||
| 727 | =item * C<yellowgreen> | ||
| 728 | |||
| 729 | =item * C<orange> | ||
| 730 | |||
| 731 | =item * C<red> | ||
| 732 | |||
| 733 | =item * C<blue> | ||
| 734 | |||
| 735 | =item * C<grey> | ||
| 736 | |||
| 737 | =item * C<lightgrey> | ||
| 738 | |||
| 739 | =item * C<gray> (alias for C<grey>) | ||
| 740 | |||
| 741 | =item * C<lightgray> (alias for C<lightgrey>) | ||
| 742 | |||
| 743 | =item * C<critical> (alias for C<red>) | ||
| 744 | |||
| 745 | =item * C<important> (alias for C<orange>) | ||
| 746 | |||
| 747 | =item * C<success> (alias for C<brightgreen>) | ||
| 748 | |||
| 749 | =item * C<informational> (alias for C<blue>) | ||
| 750 | |||
| 751 | =item * C<inactive> (alias for C<lightgrey>) | ||
| 752 | |||
| 753 | =back | ||
| 754 | |||
| 755 | =begin html | ||
| 756 | |||
| 757 | <p> | ||
| 758 | <img alt="blue" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-blue.png?raw=true"> | ||
| 759 | <img alt="brightgreen" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-brightgreen.png?raw=true"> | ||
| 760 | <img alt="green" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-green.png?raw=true"> | ||
| 761 | <img alt="grey" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-grey.png?raw=true"> | ||
| 762 | <img alt="lightgrey" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-lightgrey.png?raw=true"> | ||
| 763 | <img alt="orange" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-orange.png?raw=true"> | ||
| 764 | <img alt="red" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-red.png?raw=true"> | ||
| 765 | <img alt="yellow" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-yellow.png?raw=true"> | ||
| 766 | <img alt="yellowgreen" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/color-yellowgreen.png?raw=true"> | ||
| 767 | </p> | ||
| 768 | |||
| 769 | =end html | ||
| 770 | |||
| 771 | |||
| 772 | =head1 STYLES | ||
| 773 | |||
| 774 | Allowed styles: | ||
| 775 | |||
| 776 | =over | ||
| 777 | |||
| 778 | =item * C<flat> (default) | ||
| 779 | |||
| 780 | =item * C<flat-square> | ||
| 781 | |||
| 782 | =item * C<plastic> | ||
| 783 | |||
| 784 | =item * C<for-the-badge> | ||
| 785 | |||
| 786 | =back | ||
| 787 | |||
| 788 | =begin html | ||
| 789 | |||
| 790 | <p> | ||
| 791 | <img alt="flat" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/style-flat.png?raw=true"> | ||
| 792 | <img alt="flat-square" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/style-flat-square.png?raw=true"> | ||
| 793 | <img alt="plastic" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/style-plastic.png?raw=true"> | ||
| 794 | <img alt="for-the-badge" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/style-for-the-badge.png?raw=true"> | ||
| 795 | </p> | ||
| 796 | |||
| 797 | =end html | ||
| 798 | |||
| 799 | |||
| 800 | =head1 DEBUGGING | ||
| 801 | |||
| 802 | You can set the C<BADGE_PLUGIN_DEBUG> environment variable to get some advanced diagnostics information printed to | ||
| 803 | C<STDERR>. | ||
| 804 | |||
| 805 | BADGE_PLUGIN_DEBUG=1 | ||
| 806 | |||
| 807 | |||
| 808 | =head1 SEE ALSO | ||
| 809 | |||
| 810 | L<Mojolicious::Command::badge>, L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
| 811 | |||
| 812 | |||
| 813 | =head1 SUPPORT | ||
| 814 | |||
| 815 | =head2 Bugs / Feature Requests | ||
| 816 | |||
| 817 | Please report any bugs or feature requests through the issue tracker | ||
| 818 | at L<https://github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/issues>. | ||
| 819 | You will be notified automatically of any progress on your issue. | ||
| 820 | |||
| 821 | =head2 Source Code | ||
| 822 | |||
| 823 | This is open source software. The code repository is available for | ||
| 824 | public review and contribution under the terms of the license. | ||
| 825 | |||
| 826 | L<https://github.com/giterlizzi/perl-Mojolicious-Plugin-Badge> | ||
| 827 | |||
| 828 | git clone https://github.com/giterlizzi/perl-Mojolicious-Plugin-Badge.git | ||
| 829 | |||
| 830 | |||
| 831 | =head1 AUTHOR | ||
| 832 | |||
| 833 | =over 4 | ||
| 834 | |||
| 835 | =item * Giuseppe Di Terlizzi <gdt@cpan.org> | ||
| 836 | |||
| 837 | =back | ||
| 838 | |||
| 839 | |||
| 840 | =head1 LICENSE AND COPYRIGHT | ||
| 841 | |||
| 842 | This software is copyright (c) 2024 by Giuseppe Di Terlizzi. | ||
| 843 | |||
| 844 | This is free software; you can redistribute it and/or modify it under | ||
| 845 | the same terms as the Perl 5 programming language system itself. | ||
| 846 | |||
| 847 | =cut | ||