-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_go_fmt_for_code_in_readme.pl
executable file
·129 lines (98 loc) · 2.64 KB
/
run_go_fmt_for_code_in_readme.pl
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);
sub read_file {
my ($file_name) = @_;
open my $fh, '<', $file_name or die;
$/ = undef;
my $content = <$fh>;
close $fh;
return $content;
}
sub write_file {
my ($file_name, $content) = @_;
open my $fh, '>', $file_name or die;
print $fh $content;
close $fh;
}
sub run_go_fmt {
my ($code) = @_;
my $new_code;
my $err = 0;
write_file('a.go', $code);
my $result = `go fmt a.go 2>&1`;
if ($result =~ /^\s*\z/ || $result =~ '^\s*a.go\s*\z') {
# everything is fine
$new_code = read_file('a.go');
} else {
$new_code = '';
$err = 1;
}
unlink 'a.go';
return ($new_code, $err);
}
sub get_pretty_golang_code {
my ($code) = @_;
$code =~ s/^\s*//;
$code =~ s/\s*\z//;
$code = $code . "\n";
# At first trying to fmt code as-is
my ($pretty_code, $err) = run_go_fmt($code);
if (not $err) {
return $pretty_code;
}
# Next, trying to add 'package main'
($pretty_code, $err) = run_go_fmt("package main\n" . $code);
if (not $err) {
$pretty_code =~ s/^package main\s*//;
return $pretty_code;
}
# Next, trying to put eveyrhing in main()
($pretty_code, $err) = run_go_fmt("package main\nfunc main() {\n" . $code . "}\n");
if (not $err) {
$pretty_code =~ s/
^
package\smain
\s+
func\smain\(\)\s\{
(.*)
\}
/
$1
/sx;
$pretty_code =~ s/^\t//msg;
$pretty_code =~ s/^\s*//;
$pretty_code =~ s/\s*\z//;
$pretty_code .= "\n";
return $pretty_code;
}
# If nothing worked just returning the original trimmed code
return $code;
}
sub main {
die "Can't run with file a.go in the current directory" if -e 'a.go';
my $content = read_file('README.md');
my $new_content = '';
my $golang_code = '';
my $is_in_golang_block = 0;
foreach my $line (split /\n/, $content) {
if ($line =~ /^```\s*/ && $is_in_golang_block) {
my $pretty_golang_code = get_pretty_golang_code($golang_code);
$new_content .= "```go\n$pretty_golang_code```\n";
$golang_code = '';
$is_in_golang_block = 0;
} elsif ($line =~ /^```go\s*/) {
$is_in_golang_block = 1;
} elsif ($is_in_golang_block) {
$golang_code .= $line . "\n";
} else {
$new_content .= $line . "\n";
}
}
write_file('README.md', $new_content);
}
main();
__END__