Filter support: spaces for collapsing sequences of spaces and slashslash for striping //-style comments.

This commit is contained in:
ian%hixie.ch 2002-10-10 23:14:01 +00:00
Родитель bfe5bd2aed
Коммит b1ec5120e1
1 изменённых файлов: 63 добавлений и 2 удалений

Просмотреть файл

@ -80,9 +80,9 @@ sub include {
process($stack, $1, $2);
} elsif (/^\#\n?/os) { # comment
# ignore it
} else {
} elsif ($stack->enabled) {
# print it, including any newlines
print $_ if $stack->enabled;
print filtered($stack, $_);
}
}
close(FILE);
@ -100,6 +100,22 @@ sub process {
}
}
sub filtered {
my($stack, $text) = @_;
foreach my $filter (sort keys %{$stack->{'filters'}}) {
next unless $stack->{'filters'}->{$filter};
my $method = 'filter'->can($filter);
if (not defined($method)) {
fatal($stack, 'unknown filter', $filter);
}
$text = eval { &$method($stack, $text) };
if ($@) {
fatal($stack, "error using $filter:", $@);
}
}
return $text;
}
sub fatal {
my $stack = shift;
my $filename = $stack->{'variables'}->{'FILE'};
@ -121,6 +137,9 @@ sub new {
'FILE' => '', # source filename
'1' => 1, # for convenience
},
'filters' => {
# filters
},
'values' => [], # the value of the last condition evaluated at the nth lewel
'lastPrinting' => [], # whether we were printing at the n-1th level
'printing' => 1, # whether we are currently printing at the Nth level
@ -189,6 +208,13 @@ sub disabled {
return not $self->{'printing'};
}
sub filter {
my $self = shift;
my($filter, $value) = @_;
die "not a valid filter name: '$filter'\n" if $filter =~ m/\W/;
$self->{'filters'}->{$filter} = $value;
}
########################################################################
@ -328,4 +354,39 @@ sub include {
main::include($stack, @_);
}
sub filter {
my $stack = shift;
return if $stack->disabled;
die "argument expected\n" unless @_;
foreach (split(/\s/os, shift)) {
$stack->filter($_, 1);
}
}
sub unfilter {
my $stack = shift;
return if $stack->disabled;
die "argument expected\n" unless @_;
foreach (split(/\s/os, shift)) {
$stack->filter($_, 0);
}
}
########################################################################
package filter;
sub spaces {
my($stack, $text) = @_;
$text =~ s/ +/ /gos;
return $text;
}
sub slashslash {
my($stack, $text) = @_;
$text =~ s|//.*?(\n?)$|$1|gos;
return $text;
}
########################################################################