-
Notifications
You must be signed in to change notification settings - Fork 0
/
perl-nbsp.raku
executable file
·70 lines (57 loc) · 1.88 KB
/
perl-nbsp.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env raku
=begin overview
Run this script to correct files to use no break spaces when
xt/perl-nbsp.rakutest fails.
=end overview
use lib 'lib';
use Test-Files;
enum Syntax <CodeDoc TextDoc>;
my $degree = %*ENV<UTIL_THREADS> // 2;
sub check-line(Str $line, Syntax $state) {
given $line {
when / ^ '=begin code' / { CodeDoc }
when / ^ '=for code' / { CodeDoc }
when / ^ '=end code' / { TextDoc }
when / ^ '=' \w+ / { TextDoc }
when / ^ \s ** 4 / { CodeDoc }
default { $state }
}
}
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 and Raku 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]? ( 6 | 5 ) /\x[00A0]$0/;
$split = False;
} else {
$out-line ~~ s/ 'Perl' [ \x[0020]+ | \x[00A0] ]? $ /Perl/;
$split = True if $/;
}
$out-line ~~ s:g/ 'Perl' \x[0020] ( 6 | 5 ) /Perl\x[00A0]$0/;
$modified = True if $out-line ne $in-line;
$out-line
});
if $modified {
say "Corrected mentions of Perl and Raku 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;
}