NAME

Mojolicious::Plugin::Badge - Badge Plugin for Mojolicious

SYNOPSIS

# Mojolicious
$self->plugin('Badge');

# Mojolicious::Lite
plugin 'Badge';

get '/my-cool-badge' => sub ($c) {

  my $badge = $c->app->badge(
    label        => 'Hello',
    message      => 'Mojo!',
    color        => 'orange'
    logo         => 'https://docs.mojolicious.org/mojo/logo.png'
    badge_format => 'png',
  );

  $c->render(data => $badge, format => 'png');

};

DESCRIPTION

Mojolicious::Plugin::Badge is a Mojolicious plugin that generate "Shields.io" like badge from "badge" helper or via API URL (e.g. /badge/Hello-Mojo!-orange).

<p> <img alt="Hello Mojo!" src="https://raw.github.com/giterlizzi/perl-Mojolicious-Plugin-Badge/main/examples/hello-mojo.png?raw=true"> </p>

OPTIONS

Mojolicious::Plugin::Badge supports the following options.

disable_api

# Mojolicious::Lite
plugin 'Badge' => {disable_api => 1};

Disable the "API URL".

route

# Mojolicious::Lite
plugin 'Badge' => {route => app->routes->any('/stuff')};

Mojolicious::Routes::Route object to attach the badge API URL, defaults to generating a new one with the prefix /badge.

METHODS

Mojolicious::Plugin::Badge inherits all methods from Mojolicious::Plugin and implements the following new ones.

badge

$plugin->badge( %options );

Build and render a badge in SVG (or PNG) format.

%options

  • badge_format, Badge image format, svg (default) or png.

  • color, Message color (see "COLORS")

  • embed_logo, Includes logo in badge

  • id_suffix, The suffix of the id attributes used in the SVG's elements. Use to prevent duplicate ids if several badges are embedded on the same page.

  • label, The text that should appear on the left-hand-side of the badge

  • label_color, Label color (see "COLORS")

  • label_link, The URL that should be redirected to when the right-hand text is selected.

  • label_text_color, Label text color (see "COLORS")

  • label_title, The title attribute to associate with the left part of the badge.

  • link, Link for the whole badge (works only for SVG badge)

  • logo, A file, URL or data (e.g. "data:image/svg+xml;utf8,<svg...") representing a logo that will be displayed inside the badge.

  • message The text that should appear on the right-hand-side of the badge

  • message_link, The URL that should be redirected to when the right-hand text is selected.

  • message_text_color, Message text color (see "COLORS")

  • message_title, The title attribute to associate with the right part of the badge.

  • style, Badge style (see "STYLES")

  • title, The title attribute to associate with the entire badge. See https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title.

(label|message)_title, (label|message)_link, title and link options works only for SVG badge.

register

$plugin->register(Mojolicious->new);

Register plugin in Mojolicious application.

API URL

The default base URL is /badge. You can change the base URL via route option (see "OPTIONS").

A badge require a single path parameter with label, message and color separated by dash (-).

Example:

/badge/label-message-color

Escape

  • Underscore _ or %20 are converted to space

  • Double underscore __ is converted to underscore _

  • Double dash -- is converted to dash -

Examples:

/badge/Hello-Mojo!-green

/badge/Mojolicious--Plugin--Badge-1.0.0-green

Query parametrers

  • style, If not specified, the default style for this badge is flat (see "STYLES")

  • label, Override the default left-hand-side text

  • labelColor, Background color of the left part

  • color, Background color of the right part

  • link, Specify what clicking on the left/right of a badge should do. Note that this only works when integrating your badge in an <object> HTML tag, but not an <img> tag or a markup language.

Image format

Badge API supports svg (default) and png image formats.

Examples:

/badge/Hello-Mojo!-green
/badge/Hello-Mojo!-green.svg

/badge/Hello-Mojo!-green.png

COLORS

The badge method support named and HEX colors:

# Named color
$plugin->badge( color => 'orange', label => 'Status', message => 'Warning' );

# HEX color
$plugin->badge( color => 'fe7d37', label => 'Status', message => 'Warning' );

Allowed named colors:

  • brightgreen

  • green

  • yellow

  • yellowgreen

  • orange

  • red

  • blue

  • grey

  • lightgrey

  • gray (alias for grey)

  • lightgray (alias for lightgrey)

  • critical (alias for red)

  • important (alias for orange)

  • success (alias for brightgreen)

  • informational (alias for blue)

  • inactive (alias for lightgrey)

blue brightgreen green grey lightgrey orange red yellow yellowgreen

STYLES

Allowed styles:

  • flat (default)

  • flat-square

  • plastic

  • for-the-badge

flat flat-square plastic for-the-badge

DEBUGGING

You can set the BADGE_PLUGIN_DEBUG environment variable to get some advanced diagnostics information printed to STDERR.

BADGE_PLUGIN_DEBUG=1

SEE ALSO

Mojolicious, Mojolicious::Guides, https://mojolicious.org.