aboutsummaryrefslogtreecommitdiffstats
path: root/Perl/Webwml/TransIgnore.pm
blob: 55d3812633be376a737e1d4aa5f3a43801c32af4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/perl

##  Copyright (C) 2001  Denis Barbier <barbier@debian.org>
##  Copyright (C) 2023  Thomas Lange    <lange@debian.org>
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.

=head1 NAME

Webwml::TransIgnore - get list of files to ignore for translation

=head1 SYNOPSIS

 use Webwml::TransIgnore;
 my $ti = Webwml::TransIgnore->new("foo/bar");
 my @ignore = $ti->list();

=head1 DESCRIPTION

This module searches for F<.transignore> files in directories and returns
files listed within.  If a F<.transignore> file is found in top-level
directory, it contains files which must be ignored in all languages.

=head1 METHODS

=over 4

=cut

package Webwml::TransIgnore;

use Carp;
use File::Spec::Functions;
use Local::VCS;

use strict;
use warnings;

=item new

This is the constructor.  The argument specifies where to find the
top-level webwml directory.  If the method is called without arguments, to
top-level directory is automatically determined.

   my $ti = Webwml::TransIgnore->new("foo/bar");

=cut

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $dir   = shift;

	# init the object
	my $self  =
	{
		GLOBAL => [],
		LOCAL  => [],
		FOUND  => 0,
	};
	bless ($self, $class);

	# determine the root dir
	my $VCS = Local::VCS->new();
	my $root = shift || $VCS->get_topdir('.');

	# Read global .transignore file
	$self->_read($root);

	my @regex;
	# remove the root path from the front of the files
	for (0 .. $#{$self->{LOCAL}})
	{
               ${$self->{LOCAL}}[$_] =~ s{^$root/}{};
               push @regex, qr/${$self->{LOCAL}}[$_]/;
	}

        # global patterns are regex
        $self->{GLOBAL} = \@regex;

	# reinitialize $self->{LOCAL}
	$self->{LOCAL}  = [];
	# and read $dir/.transignore
	$self->{FOUND} = $self->_read($dir);

	return $self;
}

sub _read
{
	my $self = shift;
	my $dir  = shift;

	my $status = 0;
	my $file = catfile( $dir, '.transignore' );

	my $line;
	splice( @{$self->{LOCAL}}, 0 );

	open( my $fd, '<', $file) or return 0;
	while ( my $line = <$fd> )
	{
		next  if  $line =~ m/^#/;
		next  if  $line =~ m/^\s*$/;
		chomp $line;

		push( @{$self->{LOCAL}}, catfile($dir, $line) );
	}
	close($fd);

	return 1;
}

=item found

Return 1 if .transignore file was found, 0 otherwise.

=cut

sub found
{
	my $self = shift;
	return $self->{FOUND};
}

=item local

Return the list of files found in F<.transignore> file.

   my @ignore = $ti->local();

=cut

sub local
{
	my $self = shift;
	return $self->{LOCAL};
}

=item global

Return the list of regex defined

   my @ignore = $ti->global();

=cut

sub global
{
	my $self = shift;
	return $self->{GLOBAL};
}

=item is_local

Return 1 if argument is listed in .transignore, 0 otherwise

   my $rc = $ti->is_local("/foo/bar.wml");

=cut

sub is_local
{
	my $self  = shift;
	my $entry = shift;

	foreach my $file ( @{$self->{LOCAL}} )
	{
		return 1  if  $file eq $entry;
	}

	return 0;
}

=item is_global

Return 1 if argument has been defined, 0 otherwise.

   my $rc = $ti->is_global("/foo/bar.wml");

=cut

sub is_global
{
	my $self  = shift;
	my $entry = shift;

	foreach my $regex ( @{$self->{GLOBAL}} )
	{
               return 1  if  $entry =~ m/$regex/;
	}
	return 0;
}


=back

=head1 AUTHOR

Copyright (C) 2001  Denis Barbier <barbier@debian.org>
Copyright (C) 2023  Thomas Lange    <lange@debian.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut

1;

© 2014-2024 Faster IT GmbH | imprint | privacy policy