App-FuguWeb

 view release on metacpan or  search on metacpan

lib/App/FuguWeb/Check.pm  view on Meta::CPAN

{
	my @links = sort keys %{ $self->{external} };

	return @links;
}

# $self->run:
#	Check the site and return the problems, each one a sentence
#	that names the page. An empty list means the site is good.
#
#	Each page of a tree, the top-level 404.html and the human page
#	of each key directory take the checks of a page. The place of
#	the path of a page decides its chrome, as it does in
#	App::FuguWeb::Page. The forward takes the checks of its own
#	shape, per WEB-TOP-1 and WEB-TOP-2.
sub run ($self)
{
	$self->{external}  = {};
	$self->{inventory} = { map { $_ => 1 } $self->{config}->inventory };

	my @problems = $self->_check_inventory;

	my $forward   = App::FuguWeb::FORWARD_PAGE;
	my @not_found = grep { $_ ne $forward } $self->{config}->top_pages;

	for my $page ( $self->pages, @not_found, $self->generated_pages ) {
		next unless -f $self->{out} . "/$page";
		push @problems, $self->_check_page($page);
	}

	push @problems, $self->_check_forward
	    if -f $self->{out} . "/$forward";
	push @problems, $self->_check_nav;
	push @problems, $self->_check_outlines;
	push @problems, $self->_check_reachable;
	push @problems, $self->_check_keys;

	return @problems;
}

# $self->_check_keys:
#	Hold each key directory to the design. A description with no
#	keys block has no key directory, and the checks then find
#	nothing to say.
#
#	The rules read the source directory and not the output, so the
#	answer does not depend on a build having run. A stray key file
#	and a stale digest are faults of the checkout.
sub _check_keys ($self)
{
	my $config = $self->{config};

	return map {
		App::FuguWeb::Keys->new( config => $config, dir => $_ )
		    ->problems
	} $config->keys_dirs;
}

# $self->_check_inventory:
#	Every page and asset exists and is not empty, and the output
#	holds nothing else: no staging directory, no editor backup, no
#	stray source.
sub _check_inventory ($self)
{
	my @problems;

	unless ( -d $self->{out} ) {
		return "$self->{out}: the site is not built";
	}

	my @expected = $self->{config}->inventory;
	for my $name (@expected) {
		my $path = $self->{out} . "/$name";
		push @problems, "$name: missing from the output"
		    unless -e $path;
		push @problems, "$name: empty" if -e $path && !-s $path;
	}

	# The walk reads the whole tree. Each language tree and each
	# key directory sit below the top level, and a walk of one
	# level would take every page and every key for a stray file.
	my $entries = App::FuguWeb::list_tree( $self->{out} )
	    or return "$self->{out}: cannot read the output directory: $!";

	my %expected = map { $_ => 1 } @expected;
	push @problems, "$_: in the output but not in the site"
	    for grep { !$expected{$_} } @$entries;

	return @problems;
}

# An href of the description that passes unchanged, per WEB-LINKS-2: an
# absolute URL, and a root-absolute path, which the check of the
# references refuses, per WEB-LINKS-4.
my $UNCHANGED = qr{\A(?:[A-Za-z][A-Za-z0-9.+-]*:|/)};

# $self->_check_nav:
#	Hold each relative nav href of the description to a page of
#	each tree, per WEB-LINKS-2. The path of the href resolves from
#	each tree, and it must name a page of that tree.
sub _check_nav ($self)
{
	my $config = $self->{config};
	my @names  = $config->page_names;
	my %tree   = map {
		my $path = $_->path;
		( $path => { map { ( "$path/$_" => 1 ) } @names } )
	} $config->languages;

	my @problems;
	for my $href ( map { $_->{href} } $config->nav ) {
		next if $href =~ $UNCHANGED;

		my ($path) = App::FuguWeb::ref_parts($href);
		push @problems,
		    map { qq{nav "$href": names no page of the tree $_} }
		    grep {
			!$tree{$_}{ App::FuguWeb::resolve_ref( $_, $path )
				    // '' }
		    }
		    sort keys %tree;



( run in 1.888 second using v1.01-cache-2.11-cpan-99fa8243a42 )