File: Context.pm

package info (click to toggle)
liblog-report-lexicon-perl 1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 452 kB
  • sloc: perl: 2,676; makefile: 9
file content (162 lines) | stat: -rw-r--r-- 4,072 bytes parent folder | download
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
# This code is part of Perl distribution Log-Report-Lexicon version 1.14.
# The POD got stripped from this file by OODoc version 3.04.
# For contributors see file ChangeLog.

# This software is copyright (c) 2007-2025 by Mark Overmeer.

# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later

#oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
#oodist: This file contains OODoc-style documentation which will get stripped
#oodist: during its release in the distribution.  You can use this file for
#oodist: testing, however the code of this development version may be broken!

package Log::Report::Translator::Context;{
our $VERSION = '1.14';
}


use warnings;
use strict;

use Log::Report 'log-report-lexicon';

#--------------------

sub new(@)  { my $class = shift; (bless {}, $class)->init({@_}) }
sub init($)
{	my ($self, $args) = @_;
	$self->{LRTC_rules} = $self->_context_table($args->{rules} || {});
	$self;
}

#--------------------

sub rules() { $_[0]->{LRTC_rules} }

#--------------------


sub _strip_ctxt_spec($)
{	my $msgid = shift;
	my @tags;
	while($msgid =~ s/\{ ([^<}]*) \<(\w+) ([^}]*) \}/ length "$1$3" ? "{$1$3}" : ''/xe)
	{	push @tags, $2;
	}
	($msgid, [sort @tags]);
}

sub ctxtFor($$;$)
{	my ($self, $msg, $lang, $def_context) = @_;
	my $rawid = $msg->msgid;
	my ($msgid, $tags) = _strip_ctxt_spec $rawid;
	@$tags or return ($msgid, undef);

	my $maps = $self->rules;
	$lang    =~ s/_.*//;

	my $msg_context = $self->needDecode($rawid, $msg->context || {});
	$def_context  ||= {};
#use Data::Dumper;
#warn "context = ", Dumper $msg, $msg_context, $def_context;

	my @c;
	foreach my $tag (@$tags)
	{	my $map = $maps->{$tag}
			or error __x"no context definition for `{tag}' in `{msgid}'", tag => $tag, msgid => $rawid;

		my $set = $map->{$lang} || $map->{default};
		next if $set eq 'IGNORE';

		my $v   = $msg_context->{$tag} || $def_context->{$tag};
		unless($v)
		{	warning __x"no value for tag `{tag}' in the context", tag => $tag;
			($v) = keys %$set;
		}
		unless($set->{$v})
		{	warning __x"unknown alternative `{alt}' for tag `{tag}' in context of `{msgid}'", alt => $v, tag => $tag, msgid => $rawid;
			($v) = keys %$set;
		}

		push @c, "$tag=$set->{$v}";
	}

	my $msgctxt = join ' ', sort @c;
	($msgid, $msgctxt);
}



sub needDecode($@)
{	my ($thing,  $source) = (shift, shift);
	return +{@_} if @_ > 1;
	my $c = shift;
	return $c if !defined $c || ref $c eq 'HASH';

	my %c;
	foreach (ref $c eq 'ARRAY' ? @$c : (split /[\s,]+/, $c))
	{	my ($kw, $val) = split /\=/, $_, 2;
		defined $val
			or error __x"tags value must have form `a=b', found `{this}' in `{source}'", this => $_, source => $source;
		$c{$kw} = $val;
	}
	\%c;
}


sub expand($$@)
{	my ($self, $raw, $lang) = @_;
	my ($msgid, $tags) = _strip_ctxt_spec $raw;

	$lang =~ s/_.*//;

	my $maps    = $self->rules;
	my @options = [];

	foreach my $tag (@$tags)
	{	my $map = $maps->{$tag}
			or error __x"unknown context tag '{tag}' used in '{msgid}'", tag => $tag, msgid => $msgid;
		my $set = $map->{$lang} || $map->{default};

		my %uniq   = map +("$tag=$_" => 1), values %$set;
		my @oldopt = @options;
		@options   = ();

		foreach my $alt (keys %uniq)
		{	push @options, map +[ @$_, $alt ], @oldopt;
		}
	}

	($msgid, [sort map join(' ', @$_), @options]);
}

sub _context_table($)
{	my ($self, $rules) = @_;
	my %rules;
	foreach my $tag (keys %$rules)
	{	my $d = $rules->{$tag};
		$d = +{ alternatives => $d } if ref $d eq 'ARRAY';

		my %simple;
		my $default  = $d->{default} || {};           # default map
		if(my $alt   = $d->{alternatives})            # simpelest map
		{	$default = +{ map +($_ => $_), @$alt };
		}

		$simple{default} = $default;
		foreach my $set (keys %$d)
		{	next if $set eq 'default' || $set eq 'alternatives';
			my %set = (%$default, %{$d->{$set}});
			$simple{$_} = \%set for split /\,/, $set;  # table per lang
		}
		$rules{$tag} = \%simple;
	}

	\%rules;
}

#--------------------

1;