Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix up and refactor util/perl-nbsp.p6
Sometimes the script would replace the "6" in "Perl 6" with nothing for
whatever reason. That no longer happens. A filename can now be passed to
the script so it'll only check that file for any instances of "Perl 6"
using regular spaces.
  • Loading branch information
Kaiepi committed Mar 9, 2019
1 parent 5fdddcc commit 88232c9
Showing 1 changed file with 49 additions and 54 deletions.
103 changes: 49 additions & 54 deletions util/perl-nbsp.p6
@@ -1,68 +1,63 @@
#!/usr/bin/env perl6

use v6;
use lib 'lib';
use Test-Files;

my $degree = %*ENV<TEST_THREADS> // 2;
my @files = Test-Files.documents;
enum Syntax <CodeDoc TextDoc>;

enum Syntax (
CodeDoc => 0,
TextDoc => 1
);
my $degree = %*ENV<UTIL_THREADS> // 2;

sub check-line($line, $state) {
sub check-line(Str $line, Syntax $state) {
given $line {
when /^\=begin\scode/ { CodeDoc }
when /^\=for\scode/ { CodeDoc }
when /^\=end\scode/ { TextDoc }
when /^\=\w+/ { TextDoc }
when /^\s ** 4/ { CodeDoc }
default { $state }
when / ^ '=begin code' / { CodeDoc }
when / ^ '=for code' / { CodeDoc }
when / ^ '=end code' / { TextDoc }
when / ^ '=' \w+ / { TextDoc }
when / ^ \s ** 4 / { CodeDoc }
default { $state }
}
}

my @promises = @files.map(-> $file {
Promise.start({
my Str @in = $file.IO.lines;
my Str @out = [];
my Syntax $state = TextDoc;
my Bool $modified = False;
my Bool $split = False;
for @in -> $in-line {
unless $in-line {
@out.push($in-line);
next;
}

$state = check-line($in-line, $state);
if $state ~~ CodeDoc {
# Perl 5 and Perl 6 should keep regular spaces in code.
@out.push($in-line);
next;
}

my $out-line = $in-line;
if $split {
$out-line = $in-line.subst(/^\s?<?before 5||6>/, "\x00A0");
$split = False;
} else {
$out-line = $in-line;
$split = True if $out-line.ends-with('Perl');
}

$out-line ~~ s:g/Perl\x[0020](5||6)/Perl\x[00A0]$0/;
$modified = True if $out-line ne $in-line;
@out.push($out-line);
multi sub replace-spaces(Str $file) {
nextwith $file.IO;
}
multi sub replace-spaces(IO::Path $file) {
my Syntax $state = TextDoc;
my Bool $modified = False;
my Bool $split = False;
my Str @in = $file.lines;
my Str @out = @in.hyper(:$degree).map(anon sub (Str $in-line) {
return $in-line unless $in-line;

$state = check-line $in-line, $state;
# Perl 5 and Perl 6 should keep regular spaces in code.
return $in-line if $state == CodeDoc;

my Str $out-line = $in-line.clone;
if $split {
$out-line ~~ s/ ^ \x[0020]? <?before 6 | 5> /\x[00A0]/;
$split = False;
} else {
$out-line ~~ s/ 'Perl' [ \x[0020]+ | \x[00A0] ]? $ /Perl/;
$split = True if $/;
}

if $modified {
say "Corrected mentions of Perl 6 to use NBSP in '$file'.";
$file.IO.spurt(@out.join("\n"), :close);
$modified = False;
}
})
});
$out-line ~~ s:g/ 'Perl' \x[0020] ( 6 | 5 ) /Perl\x[00A0]$0/;
$modified = True if $out-line ne $in-line;
$out-line
});

@promises.race(:$degree);
if $modified {
say "Corrected mentions of Perl 5 and 6 to use NBSP in '$file'.";
$file.spurt(@out.join("\n"), :close);
}
}

multi sub MAIN() {
Test-Files.documents.race(:$degree).map(&replace-spaces);
}
multi sub MAIN(Str $file) {
die "$file does not exist!" unless $file.IO.e;
die "$file is a directory!" if $file.IO.d;
replace-spaces $file.IO;
}

0 comments on commit 88232c9

Please sign in to comment.