]>
Commit | Line | Data |
---|---|---|
ab81411c JH |
1 | #!/usr/bin/perl |
2 | ||
3951eeb6 ÆAB |
3 | use strict; |
4 | use warnings; | |
ab81411c | 5 | |
d2c99080 ÆAB |
6 | # Parse arguments, a simple state machine for input like: |
7 | # | |
8650c629 | 8 | # <file-to-check.txt> <valid-files-to-link-to> --section=1 git.txt git-add.txt [...] --to-lint git-add.txt a-file.txt [...] |
d2c99080 ÆAB |
9 | my %TXT; |
10 | my %SECTION; | |
11 | my $section; | |
12 | my $lint_these = 0; | |
8650c629 | 13 | my $to_check = shift @ARGV; |
d2c99080 ÆAB |
14 | for my $arg (@ARGV) { |
15 | if (my ($sec) = $arg =~ /^--section=(\d+)$/s) { | |
16 | $section = $sec; | |
17 | next; | |
18 | } | |
ab81411c | 19 | |
d2c99080 ÆAB |
20 | my ($name) = $arg =~ /^(.*?)\.txt$/s; |
21 | unless (defined $section) { | |
22 | $TXT{$name} = $arg; | |
23 | next; | |
24 | } | |
ab81411c | 25 | |
d2c99080 | 26 | $SECTION{$name} = $section; |
ab81411c JH |
27 | } |
28 | ||
d2c99080 ÆAB |
29 | my $exit_code = 0; |
30 | sub report { | |
31 | my ($pos, $line, $target, $msg) = @_; | |
32 | substr($line, $pos) = "' <-- HERE"; | |
33 | $line =~ s/^\s+//; | |
f005593d ÆAB |
34 | print STDERR "$ARGV:$.: error: $target: $msg, shown with 'HERE' below:\n"; |
35 | print STDERR "$ARGV:$.:\t'$line\n"; | |
d2c99080 | 36 | $exit_code = 1; |
ab81411c JH |
37 | } |
38 | ||
d2c99080 | 39 | @ARGV = sort values %TXT; |
8650c629 ÆAB |
40 | die "BUG: No list of valid linkgit:* files given" unless @ARGV; |
41 | @ARGV = $to_check; | |
d2c99080 ÆAB |
42 | while (<>) { |
43 | my $line = $_; | |
44 | while ($line =~ m/linkgit:((.*?)\[(\d)\])/g) { | |
45 | my $pos = pos $line; | |
46 | my ($target, $page, $section) = ($1, $2, $3); | |
ab81411c | 47 | |
d2c99080 ÆAB |
48 | # De-AsciiDoc |
49 | $page =~ s/{litdd}/--/g; | |
ab81411c | 50 | |
d2c99080 ÆAB |
51 | if (!exists $TXT{$page}) { |
52 | report($pos, $line, $target, "link outside of our own docs"); | |
53 | next; | |
54 | } | |
55 | if (!exists $SECTION{$page}) { | |
56 | report($pos, $line, $target, "link outside of our sectioned docs"); | |
57 | next; | |
58 | } | |
59 | my $real_section = $SECTION{$page}; | |
60 | if ($section != $SECTION{$page}) { | |
61 | report($pos, $line, $target, "wrong section (should be $real_section)"); | |
62 | next; | |
ab81411c JH |
63 | } |
64 | } | |
d2c99080 ÆAB |
65 | # this resets our $. for each file |
66 | close ARGV if eof; | |
ab81411c JH |
67 | } |
68 | ||
d2c99080 | 69 | exit $exit_code; |