| 1 | package App::ShellCheckWiki; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use Term::ANSIColor; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use WWW::Mechanize; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | our $VERSION = '0.03'; | |||||||||||
| 11 | |||||||||||||
| 12 | 1 | my $formats = { | |||||||||||
| 13 | '# ' => 'bold green', # H1 | ||||||||||||
| 14 | '## ' => 'bold magenta', # H2 | ||||||||||||
| 15 | '### ' => 'bold red', # H3 | ||||||||||||
| 16 | '```' => 'yellow', # Code Block | ||||||||||||
| 17 | }; | ||||||||||||
| 18 | |||||||||||||
| 19 | 1 | my $wiki_url = 'https://raw.githubusercontent.com/wiki/koalaman/shellcheck'; | |||||||||||
| 20 | 1 | my $wiki_toc = 'https://github.com/koalaman/shellcheck/wiki'; | |||||||||||
| 21 | |||||||||||||
| 22 | 1 | my $mech = WWW::Mechanize->new( autocheck => 0 ); | |||||||||||
| 23 | |||||||||||||
| 24 | 0 | ▶ | sub run { | ||||||||||
|
Subroutine
run: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 25 | 0 | my $page = $ARGV[0]; | |||||||||||
| 26 | 0 | ▶ | if (defined($page)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 0 | my ($page_id) = $page =~ m|(\d+)$|; | |||||||||||
| 28 | 0 | my $wiki_page = $wiki_url . '/SC' . $page_id . '.md'; | |||||||||||
| 29 | 0 | $mech->get($wiki_page); | |||||||||||
| 30 | 0 | ▶ | if ($mech->success) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 0 | print colored(['green'],"ShellCheck Error CS$page_id\n"); | |||||||||||
| 32 | 0 | format_page($mech->content()); | |||||||||||
| 33 | } else { | ||||||||||||
| 34 | 0 | print "Unable to find page '$page'!\n"; | |||||||||||
| 35 | 0 | show_topics(); | |||||||||||
| 36 | } | ||||||||||||
| 37 | } else { | ||||||||||||
| 38 | 0 | show_topics(); | |||||||||||
| 39 | } | ||||||||||||
| 40 | 0 | exit 0; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 0 | ▶ | sub format_page { | ||||||||||
|
Subroutine
format_page: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||
| 44 | 0 | my ($content) = @_; | |||||||||||
| 45 | 0 | my $indent = 0; | |||||||||||
| 46 | 0 | my $code_block = '```'; | |||||||||||
| 47 | 0 | ▶ | my $code_block_format = $formats->{$code_block} || 'yellow'; | ||||||||||
|
Condition0%0/2
$formats->{$code_block} || 'yellow'
MC/DC0%0/1
$formats->{$code_block} || 'yellow'
$formats->{$code_block}
Truth table0%0/2
A
A= $formats->{$code_block}
| |||||||||||||
| 48 | |||||||||||||
| 49 | 0 | for my $line ( split /\n/, $content ) { | |||||||||||
| 50 | 0 | my $matched = 0; | |||||||||||
| 51 | 0 | for my $start ( sort keys %$formats ) { | |||||||||||
| 52 | 0 | ▶ | if ( my ($remainder) = starts_with( $line, $start ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 0 | $matched = 1; | |||||||||||
| 54 | 0 | my $format = $formats->{$start}; | |||||||||||
| 55 | 0 | ▶ | if ( $start eq $code_block ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | $indent = ( $indent + 1 ) % 2; | |||||||||||
| 57 | 0 | $code_block_format = $format; | |||||||||||
| 58 | 0 | print "\n"; | |||||||||||
| 59 | } else { | ||||||||||||
| 60 | 0 | print $start; | |||||||||||
| 61 | 0 | print colored( [$format], $remainder )."\n"; | |||||||||||
| 62 | } | ||||||||||||
| 63 | } | ||||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | # Regular Line or Code Block | ||||||||||||
| 67 | 0 | ▶ | if ( not $matched ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | ▶ | if ($indent) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | # Code Block | ||||||||||||
| 70 | 0 | print colored( [$code_block_format], " $line\n" ); | |||||||||||
| 71 | } else { | ||||||||||||
| 72 | # Regular Line | ||||||||||||
| 73 | 0 | print $line . "\n"; | |||||||||||
| 74 | } | ||||||||||||
| 75 | } | ||||||||||||
| 76 | } | ||||||||||||
| 77 | 0 | print "\n"; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | sub starts_with { | ||||||||||
|
Subroutine
starts_with: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 81 | 0 | my ($line, $format) = @_; | |||||||||||
| 82 | 0 | ▶ | return unless length($line) >= length($format); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 0 | my $start = substr($line,0,length($format),''); | |||||||||||
| 84 | 0 | ▶ | return unless $start eq $format; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | return $line; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | ▶ | sub show_topics { | ||||||||||
|
Subroutine
show_topics: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 89 | 0 | print colored(['bold magenta'], "Checking for Available Topics: "); | |||||||||||
| 90 | 0 | print colored(['green'], "SCxxxx\n"); | |||||||||||
| 91 | 0 | $mech->get($wiki_toc); | |||||||||||
| 92 | 0 | my $topics = {}; | |||||||||||
| 93 | 0 | ▶ | if ($mech->success()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 0 | for my $link ($mech->links()) { | |||||||||||
| 95 | 0 | ▶ | next unless $link->url =~ m|SC(\d+)|; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 0 | $topics->{$1}++; | |||||||||||
| 97 | } | ||||||||||||
| 98 | 0 | my @ranges = number_range( keys %$topics ); | |||||||||||
| 99 | 0 | for my $range (@ranges) { | |||||||||||
| 100 | 0 | print colored(['cyan'], $range . "\n"); | |||||||||||
| 101 | } | ||||||||||||
| 102 | } else { | ||||||||||||
| 103 | 0 | print "FAIL!"; | |||||||||||
| 104 | } | ||||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub number_range { | ||||||||||
|
Subroutine
number_range: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 108 | 0 | my (@numbers) = sort { $a <=> $b } @_; | |||||||||||
| 109 | 0 | my @ranges; | |||||||||||
| 110 | 0 | my $start = shift @numbers; | |||||||||||
| 111 | 0 | my $stop = $start; | |||||||||||
| 112 | 0 | ▶ | while(scalar @numbers) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 0 | my $next = shift @numbers; | |||||||||||
| 114 | 0 | ▶ | if ( $next == $stop + 1 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 0 | $stop = $next; | |||||||||||
| 116 | } else { | ||||||||||||
| 117 | 0 | ▶ | if ($start == $stop) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | push @ranges, $start; | |||||||||||
| 119 | } else { | ||||||||||||
| 120 | 0 | push @ranges, $start . '-' . $stop; | |||||||||||
| 121 | } | ||||||||||||
| 122 | 0 | $start = $stop = $next; | |||||||||||
| 123 | } | ||||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | # Group by Leading digits; | ||||||||||||
| 127 | 0 | my $groups; | |||||||||||
| 128 | 0 | for my $range (@ranges) { | |||||||||||
| 129 | 0 | my $leading = substr($range,0,2); | |||||||||||
| 130 | 0 | push @{ $groups->{$leading} }, $range; | |||||||||||
| 131 | } | ||||||||||||
| 132 | 0 | my @output_ranges; | |||||||||||
| 133 | 0 | for my $leading ( sort keys %$groups ) { | |||||||||||
| 134 | 0 | push @output_ranges, join(',', sort @{ $groups->{$leading} }); | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | 0 | return @output_ranges; | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | 1 | 1; # End of App::ShellCheckWiki | |||||||||||
| 141 | __END__ | ||||||||||||