| 1 | package Mojolicious::Plugin::Foil; | ||||||||||||||||||||||||||||
| 2 | 1 | $Mojolicious::Plugin::Foil::VERSION = '0.006'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Mojolicious Plugin for CSS theming | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::ShareDir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use File::Slurper 'read_binary'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Image::Size; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use HTML::LinkList; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Config::Context; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 0 | my ( $self, $app, $conf ) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Append class | ||||||||||||||||||||||||||||
| 20 | 0 | push @{$app->renderer->classes}, __PACKAGE__; | |||||||||||||||||||||||||||
| 21 | 0 | push @{$app->static->classes}, __PACKAGE__; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Append directories | ||||||||||||||||||||||||||||
| 24 | # Find the Foil shared directory | ||||||||||||||||||||||||||||
| 25 | # It could be relative to the app home, | ||||||||||||||||||||||||||||
| 26 | # it could be relative to this current file, | ||||||||||||||||||||||||||||
| 27 | # it could be in a FileShared location. | ||||||||||||||||||||||||||||
| 28 | 0 | my $app_home = path($app->home); | |||||||||||||||||||||||||||
| 29 | 0 | my $foilshared = $app_home->child("foil"); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if (!-d $foilshared) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 0 | my $mydir = path(__FILE__)->parent; # lib/Mojolicious/Plugin | |||||||||||||||||||||||||||
| 34 | 0 | my $top = $mydir->parent->parent->parent; | |||||||||||||||||||||||||||
| 35 | 0 | $foilshared = $top->child("foil"); | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | if (!-d $foilshared) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | # use File::ShareDir with the distribution name | ||||||||||||||||||||||||||||
| 39 | 0 | my $dist = __PACKAGE__; | |||||||||||||||||||||||||||
| 40 | 0 | $dist =~ s/::/-/g; | |||||||||||||||||||||||||||
| 41 | 0 | my $dist_dir = path(File::ShareDir::dist_dir($dist)); | |||||||||||||||||||||||||||
| 42 | 0 | $foilshared = $dist_dir; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | push @{$app->static->paths}, $foilshared; | |||||||||||||||||||||||||||
| 47 | 0 | $self->{foilshared} = $foilshared; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | $self->_get_themes($app); | |||||||||||||||||||||||||||
| 50 | 0 | $self->_setup_concon($app,$conf); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $app->helper( 'foil_navbar' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 53 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 54 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | return $self->_make_navbar($c,%args); | |||||||||||||||||||||||||||
| 57 | 0 | } ); | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | $app->helper( 'foil_breadcrumb' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 60 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | return $self->_make_breadcrumb($c,%args); | |||||||||||||||||||||||||||
| 63 | 0 | } ); | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | $app->helper( 'foil_referrer' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 66 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | return $self->_make_referrer($c,%args); | |||||||||||||||||||||||||||
| 69 | 0 | } ); | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | $app->helper( 'foil_logo' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 71 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 72 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return $self->_make_logo_css($c,%args); | |||||||||||||||||||||||||||
| 75 | 0 | } ); | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | $app->helper( 'foil_theme_id' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 78 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | return $self->_get_theme_id($c,%args); | |||||||||||||||||||||||||||
| 81 | 0 | } ); | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | $app->helper( 'foil_theme_selector' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 84 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | return $self->_make_theme_selector($c,%args); | |||||||||||||||||||||||||||
| 87 | 0 | } ); | |||||||||||||||||||||||||||
| 88 | # add routes for setting the theme | ||||||||||||||||||||||||||||
| 89 | # and for delivering the logo | ||||||||||||||||||||||||||||
| 90 | 0 | $self->{set_route} = '/foil/set'; | |||||||||||||||||||||||||||
| 91 | 0 | $self->{logo_route} = '/foil/logo/:logo'; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $app->routes->get($self->{set_route} => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | $self->_set_theme($c); | |||||||||||||||||||||||||||
| 96 | 0 | }); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (exists $conf->{add_prefixes} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $conf->{"add_prefixes"} and defined $conf->{"add_prefixes"}
MC/DC0%0/2
exists $conf->{"add_prefixes"} and defined $conf->{"add_prefixes"}
exists $conf->{"add_prefixes"}
defined $conf->{"add_prefixes"}
Truth table0%0/3
A and B
A= exists $conf->{"add_prefixes"}B= defined $conf->{"add_prefixes"}
| |||||||||||||||||||||||||||||
| 99 | and defined $conf->{add_prefixes}) | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 0 | my @prefixes = @{$conf->{add_prefixes}}; | |||||||||||||||||||||||||||
| 102 | 0 | $self->{prefixes} = []; | |||||||||||||||||||||||||||
| 103 | 0 | foreach my $rp (@prefixes) | |||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 0 | $rp =~ s!/$!!; # remove trailing slash, if any | |||||||||||||||||||||||||||
| 106 | 0 | push @{$self->{prefixes}}, $rp; | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | $app->routes->get(${rp} . $self->{set_route} => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | $self->_set_theme($c); | |||||||||||||||||||||||||||
| 111 | 0 | }); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } # prefixes | ||||||||||||||||||||||||||||
| 114 | } # register | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub _get_themes { | ||||||||||||||||||||||||||
|
Subroutine
_get_themes: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 0 | my $app = shift; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | my $theme_file = $self->{foilshared}->child("styles/themes/themes.json"); | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (!-f $theme_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 0 | die "'$theme_file' not found"; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | my $theme_txt = path($theme_file)->slurp; | |||||||||||||||||||||||||||
| 128 | 0 | $self->{themes} = decode_json($theme_txt); | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (!defined $self->{themes}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | { | ||||||||||||||||||||||||||||
| 131 | 0 | die "'$theme_file' not parsed"; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (!defined $self->{themes}->{themes}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 0 | die "'$theme_file' not themes->themes"; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if (ref $self->{themes}->{themes} ne 'HASH') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 0 | die "'$theme_file' themes->themes not HASH " . ref $self->{themes}->{themes}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } # _get_themes | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub _setup_concon { | ||||||||||||||||||||||||||
|
Subroutine
_setup_concon: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 144 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 0 | my $app = shift; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if (!exists $app->config->{foil}->{context}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||
| 149 | 0 | return; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | my %cc_opts = ( | |||||||||||||||||||||||||||
| 153 | driver => 'ConfigGeneral', | ||||||||||||||||||||||||||||
| 154 | match_sections => [ | ||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||
| 156 | name => 'Page', | ||||||||||||||||||||||||||||
| 157 | section_type => 'page', | ||||||||||||||||||||||||||||
| 158 | match_type => 'path', | ||||||||||||||||||||||||||||
| 159 | }, | ||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||
| 161 | name => 'PageMatch', | ||||||||||||||||||||||||||||
| 162 | section_type => 'page', | ||||||||||||||||||||||||||||
| 163 | match_type => 'regex', | ||||||||||||||||||||||||||||
| 164 | }, | ||||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||||
| 166 | name => 'File', | ||||||||||||||||||||||||||||
| 167 | section_type => 'file', | ||||||||||||||||||||||||||||
| 168 | match_type => 'path', | ||||||||||||||||||||||||||||
| 169 | }, | ||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | name => 'FileMatch', | ||||||||||||||||||||||||||||
| 172 | section_type => 'file', | ||||||||||||||||||||||||||||
| 173 | match_type => 'regex', | ||||||||||||||||||||||||||||
| 174 | }, | ||||||||||||||||||||||||||||
| 175 | ], | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||
| 178 | $self->{concon} = Config::Context->new | ||||||||||||||||||||||||||||
| 179 | ( | ||||||||||||||||||||||||||||
| 180 | string => $app->config->{foil}->{context}, | ||||||||||||||||||||||||||||
| 181 | 0 | %cc_opts | |||||||||||||||||||||||||||
| 182 | ); | ||||||||||||||||||||||||||||
| 183 | } # _setup_concon | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub _get_prefix { | ||||||||||||||||||||||||||
|
Subroutine
_get_prefix: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 187 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 188 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 189 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | my $route_prefix = ''; | |||||||||||||||||||||||||||
| 192 | 0 | my $curr_url = $c->url_for('current'); | |||||||||||||||||||||||||||
| 193 | # check if this matches one of the extra routes instead | ||||||||||||||||||||||||||||
| 194 | # Note that we remember the prefix when we make the extra route | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (exists $self->{prefixes} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $self->{"prefixes"} and defined $self->{"prefixes"}
MC/DC0%0/2
exists $self->{"prefixes"} and defined $self->{"prefixes"}
exists $self->{"prefixes"}
defined $self->{"prefixes"}
Truth table0%0/3
A and B
A= exists $self->{"prefixes"}B= defined $self->{"prefixes"}
| |||||||||||||||||||||||||||||
| 196 | and defined $self->{prefixes}) | ||||||||||||||||||||||||||||
| 197 | { | ||||||||||||||||||||||||||||
| 198 | 0 | foreach my $prefix (@{$self->{prefixes}}) | |||||||||||||||||||||||||||
| 199 | { | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if ($curr_url =~ /^\Q$prefix\E\//) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | { | ||||||||||||||||||||||||||||
| 202 | 0 | $route_prefix = $prefix; | |||||||||||||||||||||||||||
| 203 | 0 | last; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | return $route_prefix; | |||||||||||||||||||||||||||
| 209 | } # _get_prefix | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub _make_theme_selector { | ||||||||||||||||||||||||||
|
Subroutine
_make_theme_selector: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 213 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 214 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 215 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | my $curr_theme = $self->_get_theme_id($c,%args); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | my $curr_url = $c->url_for('current'); | |||||||||||||||||||||||||||
| 220 | 0 | my $opt_url = $c->url_for($self->{set_route}); | |||||||||||||||||||||||||||
| 221 | 0 | my $prefix = $self->_get_prefix($c); | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | if ($prefix) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | { | ||||||||||||||||||||||||||||
| 224 | 0 | $opt_url = $c->url_for(${prefix} . $self->{set_route}); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | my @out = (); | |||||||||||||||||||||||||||
| 228 | 0 | push @out, "<div class='themes'>"; | |||||||||||||||||||||||||||
| 229 | 0 | push @out, "<form action='$opt_url'>"; | |||||||||||||||||||||||||||
| 230 | 0 | push @out, '<input type="submit" value="Select theme"/>'; | |||||||||||||||||||||||||||
| 231 | 0 | push @out, '<select name="theme">'; | |||||||||||||||||||||||||||
| 232 | 0 | my @themes = sort keys %{$self->{themes}->{themes}}; | |||||||||||||||||||||||||||
| 233 | 0 | ▶ | for (my $i=0; $i < @themes; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | { | ||||||||||||||||||||||||||||
| 235 | 0 | my $th = $themes[$i]; | |||||||||||||||||||||||||||
| 236 | 0 | ▶ | if ($th eq $curr_theme) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | { | ||||||||||||||||||||||||||||
| 238 | 0 | push @out, "<option value='$th' selected>$th</option>"; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | else | ||||||||||||||||||||||||||||
| 241 | { | ||||||||||||||||||||||||||||
| 242 | 0 | push @out, "<option value='$th'>$th</option>"; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | 0 | push @out, '</select>'; | |||||||||||||||||||||||||||
| 246 | 0 | push @out, '</form>'; | |||||||||||||||||||||||||||
| 247 | 0 | push @out, '</div>'; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | my $out = join("\n", @out); | |||||||||||||||||||||||||||
| 250 | 0 | return $out; | |||||||||||||||||||||||||||
| 251 | } # _make_theme_selector | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | ▶ | sub _make_navbar { | ||||||||||||||||||||||||||
|
Subroutine
_make_navbar: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 255 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 256 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 257 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 260 | 0 | my $foilconf = $self->_get_foilconf($c); | |||||||||||||||||||||||||||
| 261 | 0 | my $nb_host = $rhost; | |||||||||||||||||||||||||||
| 262 | 0 | ▶ | if (exists $foilconf->{navbar_host}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | { | ||||||||||||||||||||||||||||
| 264 | 0 | $nb_host = $foilconf->{navbar_host}; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 0 | my @out = (); | |||||||||||||||||||||||||||
| 267 | 0 | push @out, '<nav>'; | |||||||||||||||||||||||||||
| 268 | 0 | push @out, '<ul>'; | |||||||||||||||||||||||||||
| 269 | # we start always with Home | ||||||||||||||||||||||||||||
| 270 | 0 | push @out, "<li><a href='http://$nb_host/'>Home</a></li>"; | |||||||||||||||||||||||||||
| 271 | 0 | ▶ | if (exists $foilconf->{navbar_links}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | { | ||||||||||||||||||||||||||||
| 273 | 0 | foreach my $link (@{$foilconf->{navbar_links}}) | |||||||||||||||||||||||||||
| 274 | { | ||||||||||||||||||||||||||||
| 275 | 0 | my $name = $link; | |||||||||||||||||||||||||||
| 276 | 0 | ▶ | if ($link =~ m{(\w+)/?$}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | { | ||||||||||||||||||||||||||||
| 278 | 0 | $name = ucfirst(lc($1)); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | 0 | ▶ | if ($link =~ /^http/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | { | ||||||||||||||||||||||||||||
| 282 | 0 | push @out, "<li><a href='${link}'>$name</a></li>"; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | else | ||||||||||||||||||||||||||||
| 285 | { | ||||||||||||||||||||||||||||
| 286 | 0 | push @out, "<li><a href='http://${nb_host}${link}'>$name</a></li>"; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | 0 | push @out, '</ul>'; | |||||||||||||||||||||||||||
| 291 | 0 | push @out, '</nav>'; | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 0 | my $out = join("\n", @out); | |||||||||||||||||||||||||||
| 294 | 0 | return $out; | |||||||||||||||||||||||||||
| 295 | } # _make_navbar | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 0 | ▶ | sub _make_breadcrumb { | ||||||||||||||||||||||||||
|
Subroutine
_make_breadcrumb: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 299 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 300 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 301 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | my $breadcrumb = ''; | |||||||||||||||||||||||||||
| 304 | 0 | my $url = $c->req->url; | |||||||||||||||||||||||||||
| 305 | 0 | ▶ | if (defined $url and $url) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $url and $url
MC/DC0%0/2
defined $url and $url
defined $url
$url
Truth table0%0/3
A and B
A= defined $urlB= $url
| |||||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||
| 307 | 0 | $breadcrumb = HTML::LinkList::breadcrumb_trail(current_url=>$url, | |||||||||||||||||||||||||||
| 308 | links_head=>'', links_foot=>''); | ||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | else | ||||||||||||||||||||||||||||
| 311 | { | ||||||||||||||||||||||||||||
| 312 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 313 | 0 | my $foilconf = $self->_get_foilconf($c); | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 0 | my $hostname = $rhost; | |||||||||||||||||||||||||||
| 316 | 0 | ▶ | if (exists $foilconf->{name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | { | ||||||||||||||||||||||||||||
| 318 | 0 | $hostname = $foilconf->{name}; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | $breadcrumb = "<b>$hostname</b> <a href='/'>Home</a>"; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | 0 | return $breadcrumb; | |||||||||||||||||||||||||||
| 324 | } # _make_breadcrumb | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 0 | ▶ | sub _make_referrer { | ||||||||||||||||||||||||||
|
Subroutine
_make_referrer: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 328 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 329 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 330 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 333 | 0 | my $hostname = $rhost; | |||||||||||||||||||||||||||
| 334 | 0 | my $foilconf = $self->_get_foilconf($c); | |||||||||||||||||||||||||||
| 335 | 0 | ▶ | if (exists $foilconf->{name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | { | ||||||||||||||||||||||||||||
| 337 | 0 | $hostname = $foilconf->{name}; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | 0 | my $referrer = "<b>$hostname</b>"; | |||||||||||||||||||||||||||
| 340 | 0 | my $url = $c->req->headers->referrer; | |||||||||||||||||||||||||||
| 341 | 0 | ▶ | if (defined $url) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | { | ||||||||||||||||||||||||||||
| 343 | 0 | $referrer .= " <a href='$url'>$url</a>"; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | 0 | return $referrer; | |||||||||||||||||||||||||||
| 346 | } # _make_referrer | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 0 | ▶ | sub _make_logo_css { | ||||||||||||||||||||||||||
|
Subroutine
_make_logo_css: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 350 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 351 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 352 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | my $curr_theme = $self->_get_theme_id($c,%args); | |||||||||||||||||||||||||||
| 355 | 0 | my $logo_type = $self->{themes}->{themes}->{$curr_theme}; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | my $logo_url = $c->url_for("/styles/themes/foil_${logo_type}.png"); | |||||||||||||||||||||||||||
| 358 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 359 | 0 | my $foilconf = $self->_get_foilconf($c); | |||||||||||||||||||||||||||
| 360 | 0 | ▶ | if ($foilconf->{"${logo_type}_url"}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||
| 362 | 0 | $logo_url = $foilconf->{"${logo_type}_url"}; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | 0 | my $logo_css =<<"EOT"; | |||||||||||||||||||||||||||
| 365 | <div class="logo"><a href="/"><img src="$logo_url" alt="Home"/></a></div> | ||||||||||||||||||||||||||||
| 366 | EOT | ||||||||||||||||||||||||||||
| 367 | 0 | return $logo_css; | |||||||||||||||||||||||||||
| 368 | } # _make_logo_css | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 0 | ▶ | sub _get_theme_id { | ||||||||||||||||||||||||||
|
Subroutine
_get_theme_id: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 372 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 373 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 374 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 377 | 0 | my $theme = $c->session("theme_${rhost}"); | |||||||||||||||||||||||||||
| 378 | 0 | my $path = $c->req->url->to_abs->path; | |||||||||||||||||||||||||||
| 379 | 0 | ▶ | if (!$theme and defined $self->{concon}) # try context theme | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $theme and defined $self->{"concon"}
MC/DC0%0/2
not $theme and defined $self->{"concon"}
not $theme
defined $self->{"concon"}
Truth table0%0/3
not(A) and B
A= not $themeB= defined $self->{"concon"}
| |||||||||||||||||||||||||||||
| 380 | { | ||||||||||||||||||||||||||||
| 381 | 0 | my $cc = $self->{concon}->context(page=>$path); | |||||||||||||||||||||||||||
| 382 | 0 | ▶ | if (exists $cc->{theme}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | { | ||||||||||||||||||||||||||||
| 384 | 0 | $theme = $cc->{theme}; | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | 0 | ▶ | if (!$theme) # try default theme | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | { | ||||||||||||||||||||||||||||
| 389 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 390 | 0 | my $foilconf = $self->_get_foilconf($c); | |||||||||||||||||||||||||||
| 391 | 0 | ▶ | if ($foilconf->{default_theme}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | { | ||||||||||||||||||||||||||||
| 393 | 0 | $theme = $foilconf->{default_theme}; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | 0 | ▶ | $theme = 'silver' if !$theme; # fall back on silver | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | 0 | return $theme; | |||||||||||||||||||||||||||
| 398 | } # _get_theme_id | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 0 | ▶ | sub _set_theme { | ||||||||||||||||||||||||||
|
Subroutine
_set_theme: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 402 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 403 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 406 | 0 | my $theme = $c->param('theme'); | |||||||||||||||||||||||||||
| 407 | 0 | ▶ | if ($theme) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | { | ||||||||||||||||||||||||||||
| 409 | 0 | $c->session->{"theme_${rhost}"} = $theme; | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | 0 | my $referrer = $c->req->headers->referrer; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 0 | my $out =<<"EOT"; | |||||||||||||||||||||||||||
| 414 | <p>Current theme is "$theme".</p> | ||||||||||||||||||||||||||||
| 415 | <p>Back to: <a href='$referrer'>$referrer</a></p> | ||||||||||||||||||||||||||||
| 416 | EOT | ||||||||||||||||||||||||||||
| 417 | 0 | $c->render(template=>'foil/settings', | |||||||||||||||||||||||||||
| 418 | foil_settings=>$out); | ||||||||||||||||||||||||||||
| 419 | } # _set_theme | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 0 | ▶ | sub _get_foilconf { | ||||||||||||||||||||||||||
|
Subroutine
_get_foilconf: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 422 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 423 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 0 | my $rhost = $c->req->headers->host; | |||||||||||||||||||||||||||
| 426 | 0 | my $foilconf; | |||||||||||||||||||||||||||
| 427 | 0 | ▶ | if (exists $c->config->{foil}->{$rhost}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 428 | { | ||||||||||||||||||||||||||||
| 429 | 0 | $foilconf = $c->config->{foil}->{$rhost}; | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | else | ||||||||||||||||||||||||||||
| 432 | { | ||||||||||||||||||||||||||||
| 433 | 0 | $foilconf = $c->config->{foil}->{default}; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | 0 | return $foilconf; | |||||||||||||||||||||||||||
| 436 | } # _get_foilconf | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 1 | 1; # End of Mojolicious::Plugin::Foil | |||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =pod | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | =head1 NAME | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | Mojolicious::Plugin::Foil - Mojolicious Plugin for CSS theming | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =head1 VERSION | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | version 0.006 | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | use Mojolicious::Plugin::Foil; | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | Pretty themes; putting them in the application | ||||||||||||||||||||||||||||
| 459 | instead of in javascript; it's faster this way. | ||||||||||||||||||||||||||||
| 460 | Also other looks like breadcrumbs and other header stuff. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head1 NAME | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | Mojolicious::Plugin::Foil - looks for app | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =head1 VERSION | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | version 0.006 | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =head1 REGISTER | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | =head1 Helper Functions | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | These are functions which are NOT exported by this plugin. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =head2 _get_themes | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | Get the list of themes from the themes.json file. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =head2 _setup_concon | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | Set up the Config::Context stuff. | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =head2 _get_prefix | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | Get the "prefix" part of the current route, if it has one | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =head2 _make_theme_selector | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | For selecting themes. | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =head2 _make_navbar | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | Top-level navigation. | ||||||||||||||||||||||||||||
| 495 | The difficulty with this is that using a reverse-proxy means that | ||||||||||||||||||||||||||||
| 496 | all relative-ish URLs will be rewritten to be relative to this app. | ||||||||||||||||||||||||||||
| 497 | So we need to take account of the host the request is coming from. | ||||||||||||||||||||||||||||
| 498 | Absolute full URLs shouldn't be re-written. | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | =head2 _make_breadcrumb | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | Make breadcrumb showing the current page. | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | =head2 _make_referrer | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | Make link showing the previous page. | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =head2 _make_logo_css | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | Make logo-link which points to the Home page. | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | =head2 _get_theme_id | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | Get the ID of the current theme. | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =head2 _set_theme | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | For remembering themes. | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | =head2 _get_foilconf | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | Get the appropriate config for this rhost. | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | Kathryn Andersen <perlkat@katspace.org> | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | This software is copyright (c) 2016 by Kathryn Andersen. | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 533 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | =cut | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | __DATA__ | ||||||||||||||||||||||||||||