| 1 | package Dancer::Plugin::Preprocess::Sass; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: Generate CSS files from Sass/SCSS files | ||
| 7 | |||
| 8 | our $VERSION = '0.031'; # VERSION | ||
| 9 | |||
| 10 | use Cwd 'abs_path'; | ||
| 11 | use Dancer ':syntax'; | ||
| 12 | use Dancer::Plugin; | ||
| 13 | use File::Spec::Functions qw(catfile); | ||
| 14 | use Text::Sass; | ||
| 15 | |||
| 16 | my $settings = plugin_setting; | ||
| 17 | my $paths; | ||
| 18 | |||
| 19 | my $sass = Text::Sass->new; | ||
| 20 | my $public_dir = abs_path(setting('public') || ''); | ||
| 21 | |||
| 22 | if (exists $settings->{paths}) { | ||
| 23 | $paths = $settings->{paths}; | ||
| 24 | } | ||
| 25 | else { | ||
| 26 | $paths = ['css']; | ||
| 27 | } | ||
| 28 | |||
| 29 | # Translate URL paths to filesystem paths | ||
| 30 | my @fs_paths = map { catfile(split('/'), "") } @$paths; | ||
| 31 | |||
| 32 | if ($settings->{save}) { | ||
| 33 | # Check if the directories are writable | ||
| 34 | for my $path (@fs_paths) { | ||
| 35 | my $full_path = catfile($public_dir, $path); | ||
| 36 | if (!-w $full_path) { | ||
| 37 | warning __PACKAGE__ . ": Can't write to $full_path"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | # Make a regular expression to match URL paths | ||
| 43 | my $paths_re = join '|', map { | ||
| 44 | my $s = $_; | ||
| 45 | $s =~ s{^[^/]}{/$&}; # Add leading slash, if missing | ||
| 46 | $s =~ s{/$}{}; # Remove trailing slash | ||
| 47 | quotemeta $s; | ||
| 48 | } reverse sort @$paths; | ||
| 49 | |||
| 50 | sub _process_sass_file { | ||
| 51 | my $sass_file = shift; | ||
| 52 | my $method; | ||
| 53 | |||
| 54 | if ($sass_file =~ /\.sass$/) { | ||
| 55 | $method = 'sass2css'; | ||
| 56 | } | ||
| 57 | elsif ($sass_file =~ /\.scss$/) { | ||
| 58 | $method = 'scss2css'; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | open (my $f, '<', $sass_file); | ||
| 65 | my $contents; | ||
| 66 | { | ||
| 67 | local $/; | ||
| 68 | $contents = <$f>; | ||
| 69 | } | ||
| 70 | close($f); | ||
| 71 | |||
| 72 | return $sass->$method($contents); | ||
| 73 | } | ||
| 74 | |||
| 75 | hook before_file_render => sub { | ||
| 76 | # If saving is not enabled, then there's nothing for us to do (since we | ||
| 77 | # won't be able to save the generated CSS rules in a file, duh) | ||
| 78 | return if !$settings->{save}; | ||
| 79 | |||
| 80 | my $path = abs_path(shift); | ||
| 81 | |||
| 82 | # Build a regular expression to match filesystem paths | ||
| 83 | my $fs_paths_re = join '|', map { quotemeta } @fs_paths; | ||
| 84 | |||
| 85 | my $path_re = '^' . quotemeta(catfile($public_dir, "")) . | ||
| 86 | '(?:' . $fs_paths_re . ')'; | ||
| 87 | |||
| 88 | if ($path =~ qr{$path_re} && $path =~ qr{\.css$}) { | ||
| 89 | (my $filename = $path) =~ s/\.css$//; | ||
| 90 | my $input_file; | ||
| 91 | |||
| 92 | if ((-f ($input_file = $filename . '.sass') || | ||
| 93 | -f ($input_file = $filename . '.scss')) && | ||
| 94 | (stat($path))[9] < (stat($input_file))[9]) | ||
| 95 | { | ||
| 96 | # There is a Sass/Scss file newer than the CSS file | ||
| 97 | my $css = _process_sass_file($input_file); | ||
| 98 | |||
| 99 | if (defined $css) { | ||
| 100 | # Save the generated CSS data | ||
| 101 | open(my $f, '>', $path); | ||
| 102 | print {$f} $css; | ||
| 103 | close($f); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | }; | ||
| 108 | |||
| 109 | get qr{($paths_re)/([^/]*\.css)} => sub { | ||
| 110 | my ($path, $css_file) = splat; | ||
| 111 | |||
| 112 | # Prepend the path to the CSS file name | ||
| 113 | $path = catfile(split('/', $path)); | ||
| 114 | $css_file = catfile($path, $css_file); | ||
| 115 | |||
| 116 | # Prepend public directory location | ||
| 117 | my $css_file_abs = catfile($public_dir, $css_file); | ||
| 118 | |||
| 119 | (my $filename = $css_file_abs) =~ s/\.css$//; | ||
| 120 | my $input_file; | ||
| 121 | |||
| 122 | if (-f ($input_file = $filename . '.sass') || | ||
| 123 | -f ($input_file = $filename . '.scss')) | ||
| 124 | { | ||
| 125 | # Sass/Scss file exists | ||
| 126 | my $css = _process_sass_file($input_file); | ||
| 127 | |||
| 128 | if ($settings->{save}) { | ||
| 129 | # Saving enabled -- save the generated CSS as the requested file | ||
| 130 | open(my $f, '>', $css_file_abs); | ||
| 131 | print {$f} $css; | ||
| 132 | close($f); | ||
| 133 | } | ||
| 134 | |||
| 135 | header 'content-type' => 'text/css'; | ||
| 136 | return $css; | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | # Check if the CSS file exists. Probably not, because if it does exist, | ||
| 140 | # then Dancer should have already served it as a static file, and we | ||
| 141 | # shouldn't even end up in this route handler. Still, we'll do this | ||
| 142 | # check in case we're in some wacky parallel universe where route | ||
| 143 | # handlers are run before static files. | ||
| 144 | if (-f $css_file_abs) { | ||
| 145 | return send_file($css_file); | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | return send_error("Not found", 404); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | }; | ||
| 152 | |||
| 153 | register_plugin; | ||
| 154 | |||
| 155 | 1; # End of Dancer::Plugin::Preprocess::Sass | ||
| 156 | |||
| 157 | __END__ |