<commit_before>package Dist::Zilla::Plugin::Test::PodSpelling;
use 5.008;
use strict;
use warnings;

# VERSION

use Moose;
extends 'Dist::Zilla::Plugin::InlineFiles';
with (
	'Dist::Zilla::Role::TextTemplate',
	'Dist::Zilla::Role::FileFinderUser' => {
		default_finders => [ ':InstallModules' ],
	},
);

sub mvp_multivalue_args { return qw( stopwords ) }

has wordlist => (
	is      => 'ro',
	isa     => 'Str',
	default => 'Pod::Wordlist::hanekomu',    # default to original
);

has spell_cmd => (
	is      => 'ro',
	isa     => 'Str',
	default => '',                           # default to original
);

has stopwords => (
	is      => 'ro',
	isa     => 'ArrayRef[Str]',
	traits  => [ 'Array' ],
	default => sub { [] },                   # default to original
	handles => {
		push_stopwords => 'push',
		uniq_stopwords => 'uniq',
		no_stopwords   => 'is_empty',
	}
);

around add_file => sub {
	my ($orig, $self, $file) = @_;
	my ($set_spell_cmd, $add_stopwords, $stopwords);
	if ($self->spell_cmd) {
		$set_spell_cmd = sprintf "set_spell_cmd('%s');", $self->spell_cmd;
	}

	# automatically add author names to stopwords
	for (@{ $self->zilla->authors }) {
		local $_ = $_;    # we don't want to modify $_ in-place
		s/<.*?>//gxms;
		push @{ $self->stopwords }, /(\w{2,})/gxms;
	}

	if ( $self->zilla->copyright_holder ) {
		for ( split( ' ', $self->zilla->copyright_holder ) ) {
			my ( $word ) = $_ =~ /(\w+)/xms;

			$self->log_debug( 'copyright_holder word: ' . $word );

			$self->push_stopwords( $word );
		}
	} else {
		$self->log_debug( 'no copyright_holder found' );
	}

	foreach my $file ( @{ $self->found_files } ) {
		# many of my stopwords are part of a filename
		$self->log_debug( 'splitting filenames for more words' );

		foreach ( split( '/', $file->name ) ) {
			my ( $word ) = $_ =~ /(\w+)/xms;
			$self->log_debug( 'word: ' . $word);

			$self->push_stopwords( $word );
		}
	}

	unless ( $self->no_stopwords ) {
		$add_stopwords = 'add_stopwords(<DATA>);';
		$stopwords = join "\n", '__DATA__', $self->uniq_stopwords;
	}
	$self->$orig(
		Dist::Zilla::File::InMemory->new(
			{   name    => $file->name,
				content => $self->fill_in_string(
					$file->content,
					{
						name          => __PACKAGE__,
						wordlist      => \$self->wordlist,
						set_spell_cmd => \$set_spell_cmd,
						add_stopwords => \$add_stopwords,
						stopwords     => \$stopwords,
					},
				),
			}
		),
	);
};

__PACKAGE__->meta->make_immutable;
no Moose;
1;

# ABSTRACT: Author tests for POD spelling

=head1 SYNOPSIS

In C<dist.ini>:

	[Test::PodSpelling]

or:

	[Test::PodSpelling]
	wordlist = Pod::Wordlist
	spell_cmd = aspell list
	stopwords = CPAN
	stopwords = github
	stopwords = stopwords
	stopwords = wordlist

=head1 DESCRIPTION

This is an extension of L<Dist::Zilla::Plugin::InlineFiles>, providing
the following file:

  xt/author/pod-spell.t - a standard Test::Spelling test

=method wordlist

The module name of a word list you wish to use that works with
L<Test::Spelling>.

Defaults to L<Pod::Wordlist::hanekomu>.

=method spell_cmd

If C<spell_cmd> is set then C<set_spell_cmd( your_spell_command );> is
added to the test file to allow for custom spell check programs.

Defaults to nothing.

=method stopwords

If stopwords is set then C<add_stopwords( E<lt>DATAE<gt> )> is added
to the test file and the words are added after the C<__DATA__>
section.

C<stopwords> can appear multiple times, one word per line.

Normally no stopwords are added by default, but author names appearing in
C<dist.ini> are automatically added as stopwords so you don't have to add them
manually just because they might appear in the C<AUTHORS> section of the
generated POD document.

=begin Pod::Coverage

mvp_multivalue_args

=end Pod::Coverage

=cut
__DATA__
___[ xt/author/pod-spell.t ]___
# generated by {{ $name }}
use strict;
use warnings;
use Test::More;

eval "use Test::Spelling 0.12; use {{ $wordlist }}; 1" or die $@;

{{ $set_spell_cmd }}
{{ $add_stopwords }}
all_pod_files_spelling_ok('bin', 'lib');
{{ $stopwords }}<commit_msg>add generated by string<commit_after>package Dist::Zilla::Plugin::Test::PodSpelling;
use 5.008;
use strict;
use warnings;

# VERSION

use Moose;
extends 'Dist::Zilla::Plugin::InlineFiles';
with (
	'Dist::Zilla::Role::TextTemplate',
	'Dist::Zilla::Role::FileFinderUser' => {
		default_finders => [ ':InstallModules' ],
	},
);

sub mvp_multivalue_args { return qw( stopwords ) }

has wordlist => (
	is      => 'ro',
	isa     => 'Str',
	default => 'Pod::Wordlist::hanekomu',    # default to original
);

has spell_cmd => (
	is      => 'ro',
	isa     => 'Str',
	default => '',                           # default to original
);

has stopwords => (
	is      => 'ro',
	isa     => 'ArrayRef[Str]',
	traits  => [ 'Array' ],
	default => sub { [] },                   # default to original
	handles => {
		push_stopwords => 'push',
		uniq_stopwords => 'uniq',
		no_stopwords   => 'is_empty',
	}
);

around add_file => sub {
	my ($orig, $self, $file) = @_;
	my ($set_spell_cmd, $add_stopwords, $stopwords);
	if ($self->spell_cmd) {
		$set_spell_cmd = sprintf "set_spell_cmd('%s');", $self->spell_cmd;
	}

	# automatically add author names to stopwords
	for (@{ $self->zilla->authors }) {
		local $_ = $_;    # we don't want to modify $_ in-place
		s/<.*?>//gxms;
		push @{ $self->stopwords }, /(\w{2,})/gxms;
	}

	if ( $self->zilla->copyright_holder ) {
		for ( split( ' ', $self->zilla->copyright_holder ) ) {
			my ( $word ) = $_ =~ /(\w+)/xms;

			$self->log_debug( 'copyright_holder word: ' . $word );

			$self->push_stopwords( $word );
		}
	} else {
		$self->log_debug( 'no copyright_holder found' );
	}

	foreach my $file ( @{ $self->found_files } ) {
		# many of my stopwords are part of a filename
		$self->log_debug( 'splitting filenames for more words' );

		foreach ( split( '/', $file->name ) ) {
			my ( $word ) = $_ =~ /(\w+)/xms;
			$self->log_debug( 'word: ' . $word);

			$self->push_stopwords( $word );
		}
	}

	unless ( $self->no_stopwords ) {
		$add_stopwords = 'add_stopwords(<DATA>);';
		$stopwords = join "\n", '__DATA__', $self->uniq_stopwords;
	}
	$self->$orig(
		Dist::Zilla::File::InMemory->new(
			{   name    => $file->name,
				content => $self->fill_in_string(
					$file->content,
					{
						name          => __PACKAGE__,
						version       => \$self->VERSION
							|| 'bootstrapped version',
						wordlist      => \$self->wordlist,
						set_spell_cmd => \$set_spell_cmd,
						add_stopwords => \$add_stopwords,
						stopwords     => \$stopwords,
					},
				),
			}
		),
	);
};

__PACKAGE__->meta->make_immutable;
no Moose;
1;

# ABSTRACT: Author tests for POD spelling

=head1 SYNOPSIS

In C<dist.ini>:

	[Test::PodSpelling]

or:

	[Test::PodSpelling]
	wordlist = Pod::Wordlist
	spell_cmd = aspell list
	stopwords = CPAN
	stopwords = github
	stopwords = stopwords
	stopwords = wordlist

=head1 DESCRIPTION

This is an extension of L<Dist::Zilla::Plugin::InlineFiles>, providing
the following file:

  xt/author/pod-spell.t - a standard Test::Spelling test

=method wordlist

The module name of a word list you wish to use that works with
L<Test::Spelling>.

Defaults to L<Pod::Wordlist::hanekomu>.

=method spell_cmd

If C<spell_cmd> is set then C<set_spell_cmd( your_spell_command );> is
added to the test file to allow for custom spell check programs.

Defaults to nothing.

=method stopwords

If stopwords is set then C<add_stopwords( E<lt>DATAE<gt> )> is added
to the test file and the words are added after the C<__DATA__>
section.

C<stopwords> can appear multiple times, one word per line.

Normally no stopwords are added by default, but author names appearing in
C<dist.ini> are automatically added as stopwords so you don't have to add them
manually just because they might appear in the C<AUTHORS> section of the
generated POD document.

=begin Pod::Coverage

mvp_multivalue_args

=end Pod::Coverage

=cut
__DATA__
___[ xt/author/pod-spell.t ]___
use strict;
use warnings;
use Test::More;

# generated by {{ $name }} {{ $version }}
eval "use Test::Spelling 0.12; use {{ $wordlist }}; 1" or die $@;

{{ $set_spell_cmd }}
{{ $add_stopwords }}
all_pod_files_spelling_ok('bin', 'lib');
{{ $stopwords }}
{'fraction_non_alphanumeric': 0.1574187240283549, 'fraction_numerical': 0.0029332681495966755, 'mean_word_length': 3.491218441273326, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2388839', 'n_tokens_mistral': 3164, 'n_tokens_neox': 2951, 'n_words': 750}