package SCSite::Filter;

use Moo::Role;

requires '_filter_stream';

sub new_from_site { shift->new }

sub callback_for {
  my ($self, @args) = @_;
  sub {
    my ($stream) = @_;
    my $config = $self->_parse_config($stream->peek);
    $self->_filter_stream($stream, $config, @args)
  }
}

sub _parse_config {
  my ($self, $evt) = @_;
  my %config = ( # adapted from CSS::Tiny, extracts -scs- prefix.
    map {        # converts -scs-foo-bar: to foo_bar
      /^\s*-scs-([\w._-]+)\s*:\s*(.*?)\s*$/;
      (join('_', split '-', $1), $2);
    }
      grep { /\S/ } split /\;/, ($evt->{attrs}{style}||'')
  );
  s/^'(.*)'$/$1/ for values %config;
  \%config;
}

1;
