Skip to content

Commit d03a018

Browse files
committed
Implement tag aliaser and blocker
- add tag-aliases.json file; lets us: - alias variations of the same tag to one name - block spammy/profane tags
1 parent 424adb1 commit d03a018

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

lib/ModulesPerl6/DbBuilder/Dist/Source.pm

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package ModulesPerl6::DbBuilder::Dist::Source;
22

3+
use FindBin; FindBin->again;
34
use File::Spec::Functions qw/catfile/;
45
use JSON::Meth qw/$json/;
6+
use Mojo::JSON qw/from_json/;
57
use Mojo::UserAgent;
6-
use Mojo::Util qw/spurt decode/;
8+
use Mojo::Util qw/slurp spurt decode/;
79
use Try::Tiny;
810

911
use ModulesPerl6::DbBuilder::Log;
@@ -22,6 +24,24 @@ has _ua => InstanceOf['Mojo::UserAgent'], (
2224
is => 'lazy',
2325
default => sub { Mojo::UserAgent->new( max_redirects => 10 ) },
2426
);
27+
has _tag_aliases => Maybe[Ref['HASH']], (
28+
is => 'lazy',
29+
default => sub {
30+
my $raw_tags = eval {
31+
from_json slurp $ENV{MODULESPERL6_TAG_ALIASES_FILE}
32+
// catfile $FindBin::Bin, qw/.. tag-aliases.json/;
33+
} || do { warn "\n\nFailed to load tag-aliases.json: $@\n\n"; exit; };
34+
35+
my %tags;
36+
for my $key (keys %{ $raw_tags->{replacements} || {}}) {
37+
for (@{$raw_tags->{replacements}{$key}}) {
38+
$tags{+uc} = uc $key;
39+
}
40+
}
41+
my %no_index = map +((uc) => 1), @{ $raw_tags->{do_not_index} || [] };
42+
{ no_index => \%no_index, replacements => \%tags }
43+
}
44+
);
2545

2646
sub _download_meta {
2747
my $self = shift;
@@ -57,10 +77,16 @@ sub _parse_meta {
5777
// $json->{'repo-url'}
5878
// $json->{support}{source};
5979

80+
my ($no_index, $tags) = @{ $self->_tag_aliases }{qw/no_index replacements/};
81+
6082
$json->{tags} = [] unless ref($json->{tags}) eq 'ARRAY';
61-
@{ $json->{tags} } = map uc, map {
62-
length > 20 ? substr($_, 0, 17) . '...' : $_
63-
} grep {length and not ref} @{ $json->{tags} };
83+
@{ $json->{tags} } = map {
84+
length > 20 ? substr($_, 0, 17) . '...' : $_
85+
} map {
86+
$tags->{$_} || $_ # perform substitution to common form
87+
} grep {
88+
length and not ref and not $no_index->{$_}
89+
} map uc, @{ $json->{tags} };
6490
return $self->_fill_missing( {%$json} );
6591
}
6692

@@ -435,4 +461,3 @@ Original version of this module was written by Zoffix Znet
435461
=head1 LICENSE
436462
437463
You can use and distribute this module under the same terms as Perl itself.
438-

tag-aliases.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"replacements": {
3+
"database": ["dbdish", "dbish", "odbc"],
4+
"filesystem": ["file"],
5+
"json": ["json-c"],
6+
"IPv4/IPv6": ["IP", "IPv4", "IPv6", "IP address"],
7+
"testing": ["test"],
8+
"utils": ["util", "utilities", "utility"],
9+
"users": ["user"],
10+
"url": ["uri"],
11+
"web": ["web service", "webservice", "p6sgi", "websocket"]
12+
},
13+
14+
"do_not_index": [
15+
"***TAGS IN THIS LIST ARE ONES MARKED NOT TO BE INDEXED***",
16+
"football-data.org"
17+
]
18+
}

0 commit comments

Comments
 (0)