--- /dev/null
+package Newslash::Plugin::CSSCompile;
+use Mojo::Base 'Mojolicious::Plugin';
+use Mojo::Home;
+use File::Spec;
+
+#use Data::Dumper;
+#use Mojo::Log;
+#my $log = Mojo::Log->new;
+
+my $LESSC = '/usr/bin/lessc';
+
+sub register {
+ my ($self, $app, $conf) = @_;
+
+ $self->{source_dir} = $conf->{source_dir} || $app->home->rel_dir('/css');
+ $self->{dest_dir} = $conf->{dest_dir} || $app->home->rel_dir('public/css');
+
+ $self->compile;
+}
+
+sub compile {
+ my ($self) = @_;
+
+ opendir(my $dh, $self->{source_dir});
+
+ while(my $fname = readdir $dh) {
+ # TODO: find subdirectory
+ if ($fname =~ m/^(.*)\.less$/) {
+ my $basename = $1;
+ my $pathname = File::Spec->catfile($self->{source_dir}, $fname);
+ my $dest = File::Spec->catfile($self->{dest_dir}, $basename . '.css');
+
+ my $less = $self->less_compile_file($pathname);
+ $self->store($less, $dest);
+ } elsif ($fname =~ m/^(.*)\.css$/) {
+ my $basename = $1;
+ my $pathname = File::Spec->catfile($self->{source_dir}, $fname);
+ my $dest = File::Spec->catfile($self->{dest_dir}, $basename . '.css');
+
+ my $fh = FileHandle->new;
+ if ($fh->open($pathname, "r")) {
+ my $css = do { local $/; <$fh> };
+ $fh->close;
+ $self->store($css, $dest);
+ }
+ }
+ }
+}
+
+sub store {
+ my ($self, $contents, $dest) = @_;
+ my $fh = FileHandle->new;
+ if ($fh->open($dest, "w")) {
+ $fh->print($contents);
+ $fh->close;
+ return 1;
+ }
+ return 0;
+}
+
+sub less_compile_file {
+ my ($self, $pathname) = @_;
+ my $fh = FileHandle->new;
+
+ if ($fh->open("$LESSC $pathname 2>/dev/null |")) {
+ my $result = do { local $/; <$fh> };
+ return $result;
+ }
+ return undef;
+}
+
+
+1;