Skip to content

Instantly share code, notes, and snippets.

@rns
Last active December 31, 2015 20:49
Show Gist options
  • Save rns/8042426 to your computer and use it in GitHub Desktop.
Save rns/8042426 to your computer and use it in GitHub Desktop.
wrap C<X> as L<C<X>> if X is =head? X
#!/usr/bin/perl
# Copyright 2013 Jeffrey Kegler
# This file is part of Marpa::R2. Marpa::R2 is free software: you can
# redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Marpa::R2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser
# General Public License along with Marpa::R2. If not, see
# http://www.gnu.org/licenses/.
use 5.010;
use strict;
use warnings;
use English qw( -no_match_vars );
use Fatal qw( open close );
use Carp;
use Pod::Simple;
use Test::Pod;
use Test::More;
# Test that the module passes perlcritic
BEGIN {
$OUTPUT_AUTOFLUSH = 1;
}
my %exclude = map { ( $_, 1 ) } qw(
inc/Test/Weaken.pm
);
open my $manifest, '<', 'MANIFEST'
or Marpa::R2::exception("open of MANIFEST failed: $ERRNO");
my @test_files = ();
FILE: while ( my $file = <$manifest> ) {
chomp $file;
$file =~ s/\s*[#].*\z//xms;
next FILE if -d $file;
next FILE if $exclude{$file};
my ($ext) = $file =~ / [.] ([^.]+) \z /xms;
next FILE if not defined $ext;
$ext = lc $ext;
given ($ext) {
when ('pl') { push @test_files, $file }
when ('pod') { push @test_files, $file }
when ('t') { push @test_files, $file }
when ('pm') { push @test_files, $file }
} ## end given
} # FILE
close $manifest;
Test::Pod::all_pod_files_ok(@test_files);
# check for C<X>s for =head X are not L<C<X>>
package My::LinkChecker;
use Pod::Simple;
our @ISA = qw(Pod::Simple);
my ($en, $att);
my %heads;
my %Cs;
my %Ls;
my $start_line = undef;
sub _handle_element_start {
my($parser, $element_name, $attr_hash_r) = @_;
($en, $att) = ($element_name, $attr_hash_r);
}
sub _handle_text{
my ( $parser, $text ) = @_;
if ($en =~ /^head/){
$start_line = $att->{start_line};
# say "$en at $start_line";
push @{ $heads{$text} }, $start_line;
}
elsif ($en eq 'Para'){
$start_line = $att->{start_line};
# say "$en at $start_line";
}
elsif ($en eq 'C'){
$Cs{$start_line}->{$text} = undef;
# say "C<$text> at $start_line";
}
elsif ($en eq 'L'){
$Ls{$start_line}->{$text} = undef;
# say "L<$text> at $start_line";
}
}
# TODOs
# don't report
# C<X>'s if they directly (by a certain up to 10 number of lines) follow =head? X
# i.we. if C<X> are in the same section as =head? X
# L<C<next()> method>
sub check_Cs {
my $self = shift;
my $fn = $self->source_filename;
$fn =~ s{/}{\\}g;
# use YAML;
# say Dump \%heads, \%Ls, \%Cs;
# delete Cs which have Ls on the same line
while (my ($line, $texts) = each %Cs){
delete $Cs{$line} if exists $Ls{$line};
}
# check if the rest Cs are in heads
for my $line (sort { $a <=> $b } keys %Cs){
my @texts = sort keys %{ $Cs{$line} };
for my $text (@texts){
if (exists $heads{$text}){
say "C<$text> at " . "C:\\cygwin\\home\\Ruslan\\Marpa--R2\\cpan\\" . $fn
. " line $line\n\tis the same as =head at line(s): "
. join(', ', @{ $heads{$text} })
. ", but is not wrapped as\n\tL<C<$text>|/\"$text\">";
}
}
}
undef %heads;
undef %Cs;
undef %Ls;
}
package main;
for my $test_file (@test_files){
next unless $test_file =~ /\.pod$/
and $test_file !~ m{NAIF/}
and $test_file =~ m{^pod/}
;
# say "\n# $test_file";
my $mlc = My::LinkChecker->new;
$mlc->parse_file($test_file);
$mlc->check_Cs;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment