blib/lib/App/Netdisco/Web/Theme.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 139 CC 11 SCAR 23.8
CC
11
Cov
0%
resolve_configured_theme
30.0
find_theme_file
12.0
is_reserved_theme_name
6.0
CRAP 10.8
1 package App::Netdisco::Web::Theme;
2  
3 use Dancer ':syntax';
4 use Path::Class qw/dir file/;
5 use App::Netdisco::Util::SiteLocal 'site_local_paths';
6  
7 use base 'Exporter';
8 our @EXPORT_OK = qw/find_theme_file theme_problem_message theme_scope_message theme_sheet_name/;
9  
10 # A theme name becomes a file name, so nothing that could leave the directory.
11 my $VALID_NAME = qr/\A[A-Za-z0-9_-]+\z/;
12  
13 # light and auto are reserved. light is the standard colors, so it needs no
14 # sheet (undef). auto follows the browser's color scheme: it serves the dark
15 # sheet, whose rules apply only once the page's resolver sets data-bs-theme="dark".
16 my %RESERVED_SHEET = (light => undef, auto => 'dark');
17  
18 sub is_reserved_theme_name {
19 my $name = shift;
20 return (defined $name and exists $RESERVED_SHEET{$name}) ? 1 : 0;
21 }
22  
23 sub theme_sheet_name {
24 my $name = shift;
25 return $RESERVED_SHEET{$name} if is_reserved_theme_name($name);
26 return $name;
27 }
28  
29 sub find_theme_file {
30 my ($name, @dirs) = @_;
31 return undef unless defined $name and $name =~ $VALID_NAME;
32  
33 foreach my $dir (@dirs) {
34 my $candidate = file($dir, "$name.css");
35 return $candidate->stringify if -f $candidate;
36 }
37 return undef;
38 }
39  
40 sub theme_problem_message {
41 my ($name, @dirs) = @_;
42  
43 return sprintf q{web_theme '%s' is not a valid theme name. Use letters, digits, }
44 . q{'-' and '_' only, or remove web_theme to use the default palette.}, $name
45 unless $name =~ $VALID_NAME;
46  
47 return sprintf q{web_theme is '%s' but no %s.css was found in %s. Add the file }
48 . q{to one of those directories, or remove web_theme to use the default palette.},
49 $name, $name, join(', ', @dirs);
50 }
51  
52 sub theme_scope_message {
53 my ($name, $path, $css) = @_;
54 return undef if $css =~ m/\[\s*data-bs-theme\s*=\s*(["']?)\Q$name\E\1\s*\]/;
55  
56 return sprintf q{web_theme '%s' was loaded from %s, but none of its rules are }
57 . q{scoped to [data-bs-theme="%s"], so Bootstrap will not apply them. Scope }
58 . q{the rules to that selector, or set web_theme to the name the file uses.},
59 $name, $path, $name;
60 }
61  
62 sub theme_dirs {
63 return (
64 dir(setting('public'), 'css', 'themes')->stringify,
65 map { dir($_, 'themes')->stringify } site_local_paths(),
66 );
67 }
68  
69 sub reserved_name_message {
70 my ($name, $path) = @_;
71 return sprintf q{a site theme named '%s' was found at %s, but '%s' is a reserved }
72 . q{web_theme value and the file is not used. Rename the file and set web_theme }
73 . q{to the new name.}, $name, $path, $name;
74 }
75  
76 sub warn_about_reserved_site_files {
77 my @site_dirs = map { dir($_, 'themes')->stringify } site_local_paths();
78 foreach my $reserved (sort keys %RESERVED_SHEET) {
79 my $path = find_theme_file($reserved, @site_dirs) or next;
80 warning reserved_name_message($reserved, $path);
81 }
82 }
83  
84 sub resolve_configured_theme {
85 setting('_web_theme' => undef);
86 warn_about_reserved_site_files();
87 my $name = setting('web_theme');
88 return unless defined $name and length $name;
89 return if $name eq 'light';
90  
91 my $sheet = theme_sheet_name($name);
92 my @dirs = theme_dirs();
93 my $path = find_theme_file($sheet, @dirs);
94 return warning theme_problem_message($name, @dirs) unless $path;
95  
96 my $css = scalar file($path)->slurp;
97 my $scope = theme_scope_message($sheet, $path, $css);
98 warning $scope if $scope;
99  
100 setting('_web_theme' => {
101 name => $name,
102 path => $path,
103 mtime => (stat $path)[9],
104 });
105 }
106  
107 get '/theme.css' => sub {
108 my $theme = setting('_web_theme');
109 # No error page: this route needs no login, and with show_errors set an
110 # error page lists the application's settings.
111 unless ($theme and -f $theme->{path}) {
112 status 'not_found';
113 return '';
114 }
115 send_file $theme->{path}, system_path => 1, content_type => 'text/css';
116 };
117  
118 get '/theme/*.css' => sub {
119 my ($name) = splat;
120 my $path = is_reserved_theme_name($name) ? undef : find_theme_file($name, theme_dirs());
121 # No error page, for the same reason as /theme.css. A reserved name never
122 # reaches the file lookup, so a site file of that name cannot be served.
123 unless ($path) {
124 status 'not_found';
125 return '';
126 }
127 send_file $path, system_path => 1, content_type => 'text/css';
128 };
129  
130 true;