Skip to content

Commit

Permalink
Added kibana doc link checker
Browse files Browse the repository at this point in the history
Closes #77
  • Loading branch information
clintongormley committed Aug 5, 2016
1 parent 77b07a8 commit 9ead0bf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 32 deletions.
61 changes: 55 additions & 6 deletions build_docs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,66 @@ sub build_all {
$_->{redirect} );
}

my $links = ES::LinkCheck->new($build_dir);
check_links($build_dir);

if ( $links->check ) {
say $links->report;
push_changes($build_dir)
if $Opts->{push};
}

#===================================
sub check_links {
#===================================
my $build_dir = shift;
my $link_checker = ES::LinkCheck->new($build_dir);

$link_checker->check;

check_kibana_links( $build_dir, $link_checker );
if ( $link_checker->has_bad ) {
say $link_checker->report;
}
else {
die $links->report;
die $link_checker->report;
}

push_changes($build_dir)
if $Opts->{push};
}

#===================================
sub check_kibana_links {
#===================================
my $build_dir = shift;
my $link_checker = shift;
my $branch;

say "Checking Kibana links";

my $re = qr|`\$\{baseUrl\}guide/(.+)\$\{urlVersion\}([^#`]+)(?:#([^`*]))?`|;

my $extractor = sub {
my $contents = shift;
return sub {
while ( $contents =~ m{$re}g ) {
return ( $1 . $branch . $2, $3 );
}
return;
};

};

my $src_path = 'src/ui/public/documentation_links/documentation_links.js';
my $repo = ES::Repo->get_repo('kibana');

my @branches = sort map { $_->basename }
grep { $_->is_dir } $build_dir->subdir('en/kibana')->children;

for (@branches) {
$branch = $_;
next if $branch eq 'current' || $branch =~ /^\d/ && $branch < 5;
say " Branch $branch";
$repo->checkout( "link_check", $branch );
$link_checker->check_file( $repo->dir->file($src_path),
$extractor, "Kibana [$branch]: $src_path" );
}
}

#===================================
Expand Down
55 changes: 29 additions & 26 deletions lib/ES/LinkCheck.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sub check {
my $self = shift;
my $dir = $self->root;

$dir->recurse(
$self->root->recurse(
callback => sub {
my $item = shift;

Expand All @@ -34,49 +34,51 @@ sub check {
if $item->basename eq 'images';
return;
}
$self->_check_links( $dir, $item )
$self->check_file($item)
if $item->basename =~ /\.html$/;
}
);
return !keys %{ $self->bad };
return $self->has_bad;

}

#===================================
sub check_file {
#===================================
my $self = shift;
my $file = shift;
my $dir = $self->root;

$self->_check_links( $dir, $file );
return !keys %{ $self->bad };

}

#===================================
sub _check_links {
#===================================
my ( $self, $dir, $file ) = @_;
my ( $self, $file, $extract, $file_descr ) = @_;
$extract||=\&_link_extractor;
$file_descr ||= "$file";

my $contents = $file->slurp( iomode => '<:encoding(UTF-8)' );
my $seen = $self->seen;
my $link_it = $extract->($contents);
my $seen = $self->seen;

while ( my ( $path, $fragment ) = $link_it->() ) {

while ( $contents =~ m{$Link_Re}g ) {
my $path = $1;
my $fragment = $2;
my $dest = $dir->file($path);
my $dest = $self->root->file($path);
unless ( $self->_file_exists( $dest, $path ) ) {
$self->add_bad( $file, $path );
$self->add_bad( $file_descr, $path );
next;
}
next unless $fragment;
unless ( $self->_fragment_exists( $dest, $path, $fragment ) ) {
$self->add_bad( $file, "$path#$fragment" );
$self->add_bad( $file_descr, "$path#$fragment" );
}
}
}

#===================================
sub _link_extractor {
#===================================
my $contents = shift;
return sub {
while ( $contents =~ m{$Link_Re}g ) {
return ( $1, $2 );
}
return;
};
}

#===================================
sub report {
#===================================
Expand Down Expand Up @@ -130,9 +132,10 @@ sub add_bad {
}

#===================================
sub root { shift->{root} }
sub seen { shift->{seen} }
sub bad { shift->{bad} }
sub root { shift->{root} }
sub seen { shift->{seen} }
sub bad { shift->{bad} }
sub has_bad { !keys %{ shift->bad } }
#===================================

1

0 comments on commit 9ead0bf

Please sign in to comment.