Skip to content

Commit 88232c9

Browse files
committed
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.
1 parent 5fdddcc commit 88232c9

File tree

1 file changed

+49
-54
lines changed

1 file changed

+49
-54
lines changed

util/perl-nbsp.p6

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,63 @@
11
#!/usr/bin/env perl6
2-
32
use v6;
43
use lib 'lib';
54
use Test-Files;
65

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

10-
enum Syntax (
11-
CodeDoc => 0,
12-
TextDoc => 1
13-
);
8+
my $degree = %*ENV<UTIL_THREADS> // 2;
149

15-
sub check-line($line, $state) {
10+
sub check-line(Str $line, Syntax $state) {
1611
given $line {
17-
when /^\=begin\scode/ { CodeDoc }
18-
when /^\=for\scode/ { CodeDoc }
19-
when /^\=end\scode/ { TextDoc }
20-
when /^\=\w+/ { TextDoc }
21-
when /^\s ** 4/ { CodeDoc }
22-
default { $state }
12+
when / ^ '=begin code' / { CodeDoc }
13+
when / ^ '=for code' / { CodeDoc }
14+
when / ^ '=end code' / { TextDoc }
15+
when / ^ '=' \w+ / { TextDoc }
16+
when / ^ \s ** 4 / { CodeDoc }
17+
default { $state }
2318
}
2419
}
2520

26-
my @promises = @files.map(-> $file {
27-
Promise.start({
28-
my Str @in = $file.IO.lines;
29-
my Str @out = [];
30-
my Syntax $state = TextDoc;
31-
my Bool $modified = False;
32-
my Bool $split = False;
33-
for @in -> $in-line {
34-
unless $in-line {
35-
@out.push($in-line);
36-
next;
37-
}
38-
39-
$state = check-line($in-line, $state);
40-
if $state ~~ CodeDoc {
41-
# Perl 5 and Perl 6 should keep regular spaces in code.
42-
@out.push($in-line);
43-
next;
44-
}
45-
46-
my $out-line = $in-line;
47-
if $split {
48-
$out-line = $in-line.subst(/^\s?<?before 5||6>/, "\x00A0");
49-
$split = False;
50-
} else {
51-
$out-line = $in-line;
52-
$split = True if $out-line.ends-with('Perl');
53-
}
54-
55-
$out-line ~~ s:g/Perl\x[0020](5||6)/Perl\x[00A0]$0/;
56-
$modified = True if $out-line ne $in-line;
57-
@out.push($out-line);
21+
multi sub replace-spaces(Str $file) {
22+
nextwith $file.IO;
23+
}
24+
multi sub replace-spaces(IO::Path $file) {
25+
my Syntax $state = TextDoc;
26+
my Bool $modified = False;
27+
my Bool $split = False;
28+
my Str @in = $file.lines;
29+
my Str @out = @in.hyper(:$degree).map(anon sub (Str $in-line) {
30+
return $in-line unless $in-line;
31+
32+
$state = check-line $in-line, $state;
33+
# Perl 5 and Perl 6 should keep regular spaces in code.
34+
return $in-line if $state == CodeDoc;
35+
36+
my Str $out-line = $in-line.clone;
37+
if $split {
38+
$out-line ~~ s/ ^ \x[0020]? <?before 6 | 5> /\x[00A0]/;
39+
$split = False;
40+
} else {
41+
$out-line ~~ s/ 'Perl' [ \x[0020]+ | \x[00A0] ]? $ /Perl/;
42+
$split = True if $/;
5843
}
5944

60-
if $modified {
61-
say "Corrected mentions of Perl 6 to use NBSP in '$file'.";
62-
$file.IO.spurt(@out.join("\n"), :close);
63-
$modified = False;
64-
}
65-
})
66-
});
45+
$out-line ~~ s:g/ 'Perl' \x[0020] ( 6 | 5 ) /Perl\x[00A0]$0/;
46+
$modified = True if $out-line ne $in-line;
47+
$out-line
48+
});
6749

68-
@promises.race(:$degree);
50+
if $modified {
51+
say "Corrected mentions of Perl 5 and 6 to use NBSP in '$file'.";
52+
$file.spurt(@out.join("\n"), :close);
53+
}
54+
}
55+
56+
multi sub MAIN() {
57+
Test-Files.documents.race(:$degree).map(&replace-spaces);
58+
}
59+
multi sub MAIN(Str $file) {
60+
die "$file does not exist!" unless $file.IO.e;
61+
die "$file is a directory!" if $file.IO.d;
62+
replace-spaces $file.IO;
63+
}

0 commit comments

Comments
 (0)