You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
603 lines
18 KiB
603 lines
18 KiB
7 years ago
|
package Anvil::Tools::Words;
|
||
8 years ago
|
#
|
||
|
# This module contains methods used to handle storage related tasks
|
||
|
#
|
||
|
|
||
|
use strict;
|
||
|
use warnings;
|
||
|
use Data::Dumper;
|
||
8 years ago
|
use XML::Simple qw(:strict);
|
||
7 years ago
|
use Scalar::Util qw(weaken isweak);
|
||
8 years ago
|
|
||
|
our $VERSION = "3.0.0";
|
||
|
my $THIS_FILE = "Words.pm";
|
||
|
|
||
|
# Setup for UTF-8 mode.
|
||
|
# use utf8;
|
||
|
# $ENV{'PERL_UNICODE'} = 1;
|
||
|
|
||
|
### Methods;
|
||
8 years ago
|
# clean_spaces
|
||
8 years ago
|
# key
|
||
8 years ago
|
# language
|
||
8 years ago
|
# read
|
||
8 years ago
|
# string
|
||
8 years ago
|
|
||
|
=pod
|
||
|
|
||
|
=encoding utf8
|
||
|
|
||
|
=head1 NAME
|
||
|
|
||
7 years ago
|
Anvil::Tools::Words
|
||
8 years ago
|
|
||
|
Provides all methods related to generating translated strings for users.
|
||
|
|
||
|
=head1 SYNOPSIS
|
||
|
|
||
7 years ago
|
use Anvil::Tools;
|
||
8 years ago
|
|
||
7 years ago
|
# Get a common object handle on all Anvil::Tools modules.
|
||
|
my $anvil = Anvil::Tools->new();
|
||
8 years ago
|
|
||
7 years ago
|
# Access to methods using '$anvil->Words->X'.
|
||
8 years ago
|
#
|
||
|
# Example using 'read()';
|
||
7 years ago
|
my $foo_path = $anvil->Words->read({file => $anvil->data->{path}{words}{'anvil.xml'}});
|
||
8 years ago
|
|
||
|
=head1 METHODS
|
||
|
|
||
|
Methods in this module;
|
||
|
|
||
|
=cut
|
||
|
sub new
|
||
|
{
|
||
|
my $class = shift;
|
||
8 years ago
|
my $self = {
|
||
|
WORDS => {
|
||
|
LANGUAGE => "",
|
||
|
},
|
||
|
};
|
||
8 years ago
|
|
||
|
bless $self, $class;
|
||
|
|
||
|
return ($self);
|
||
|
}
|
||
|
|
||
7 years ago
|
# Get a handle on the Anvil::Tools object. I know that technically that is a sibling module, but it makes more
|
||
8 years ago
|
# sense in this case to think of it as a parent.
|
||
|
sub parent
|
||
|
{
|
||
|
my $self = shift;
|
||
|
my $parent = shift;
|
||
|
|
||
|
$self->{HANDLE}{TOOLS} = $parent if $parent;
|
||
|
|
||
7 years ago
|
# Defend against memory leads. See Scalar::Util'.
|
||
|
if (not isweak($self->{HANDLE}{TOOLS}))
|
||
|
{
|
||
|
weaken($self->{HANDLE}{TOOLS});;
|
||
|
}
|
||
|
|
||
8 years ago
|
return ($self->{HANDLE}{TOOLS});
|
||
|
}
|
||
|
|
||
|
|
||
|
#############################################################################################################
|
||
|
# Public methods #
|
||
|
#############################################################################################################
|
||
|
|
||
8 years ago
|
=head2 clean_spaces
|
||
|
|
||
|
This methid takes a string via a 'C<< line >>' parameter and strips leading and trailing spaces, plus compresses multiple spaces into single spaces. It is designed primarily for use by code parsing text coming in from a shell command.
|
||
|
|
||
7 years ago
|
my $line = $anvil->Words->clean_spaces({ string => $_ });
|
||
8 years ago
|
|
||
|
Parameters;
|
||
|
|
||
|
=head3 string (required)
|
||
|
|
||
|
This sets the string to be cleaned. If it is not passed in, or if the string is empty, then an empty string will be returned without error.
|
||
|
|
||
|
=cut
|
||
|
sub clean_spaces
|
||
|
{
|
||
|
my $self = shift;
|
||
|
my $parameter = shift;
|
||
7 years ago
|
my $anvil = $self->parent;
|
||
8 years ago
|
|
||
|
# Setup default values
|
||
|
my $string = defined $parameter->{string} ? $parameter->{string} : "";
|
||
|
$string =~ s/^\s+//;
|
||
|
$string =~ s/\s+$//;
|
||
|
$string =~ s/\s+/ /g;
|
||
|
|
||
|
return($string);
|
||
|
}
|
||
|
|
||
8 years ago
|
=head2 key
|
||
|
|
||
7 years ago
|
NOTE: This is likely not the method you want. This method does no parsing at all. It returns the raw string from the 'words' file. You probably want C<< $anvil->Words->string() >> if you want to inject variables and get a string back ready to display to the user.
|
||
8 years ago
|
|
||
|
This returns a string by its key name. Optionally, a language and/or a source file can be specified. When no file is specified, loaded files will be search in alphabetical order (including path) and the first match is returned.
|
||
|
|
||
|
If the requested string is not found, 'C<< #!not_found!# >>' is returned.
|
||
|
|
||
|
Example to retrieve 'C<< t_0001 >>';
|
||
|
|
||
7 years ago
|
my $string = $anvil->Words->key({key => 't_0001'});
|
||
8 years ago
|
|
||
|
Same, but specifying the key from Canadian english;
|
||
|
|
||
7 years ago
|
my $string = $anvil->Words->key({
|
||
8 years ago
|
key => 't_0001',
|
||
|
language => 'en_CA',
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
|
Same, but specifying a source file.
|
||
|
|
||
7 years ago
|
my $string = $anvil->Words->key({
|
||
8 years ago
|
key => 't_0001',
|
||
|
language => 'en_CA',
|
||
7 years ago
|
file => 'anvil.xml',
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
|
Parameters;
|
||
|
|
||
8 years ago
|
=head3 file (optional)
|
||
|
|
||
|
This is the specific file to read the string from. It should generally not be needed as string keys should not be reused. However, if it happens, this is a way to specify which file's version you want.
|
||
|
|
||
7 years ago
|
The file can be the file name, or a path. The specified file is search for by matching the the passed in string against the end of the file path. For example, 'C<< file => 'AN/anvil.xml' >> will match the file 'c<< /usr/share/perl5/AN/anvil.xml >>'.
|
||
8 years ago
|
|
||
8 years ago
|
=head3 key (required)
|
||
|
|
||
|
This is the key to return the string for.
|
||
|
|
||
|
=head3 language (optional)
|
||
|
|
||
|
This is the ISO code for the language you wish to read. For example, 'en_CA' to get the Canadian English string, or 'jp' for the Japanese string.
|
||
|
|
||
8 years ago
|
When no language is passed, 'C<< Words->language >>' is used.
|
||
8 years ago
|
|
||
|
=cut
|
||
|
sub key
|
||
|
{
|
||
|
my $self = shift;
|
||
|
my $parameter = shift;
|
||
7 years ago
|
my $anvil = $self->parent;
|
||
8 years ago
|
|
||
|
# Setup default values
|
||
|
my $key = defined $parameter->{key} ? $parameter->{key} : "";
|
||
7 years ago
|
my $language = defined $parameter->{language} ? $parameter->{language} : $anvil->Words->language;
|
||
8 years ago
|
my $file = defined $parameter->{file} ? $parameter->{file} : "";
|
||
|
my $string = "#!not_found!#";
|
||
|
my $error = 0;
|
||
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - key: [$key], language: [$language], file: [$file]\n";
|
||
|
|
||
|
if (not $key)
|
||
|
{
|
||
7 years ago
|
#print $THIS_FILE." ".__LINE__."; Anvil::Tools::Words->key()' called without a key name to read.\n";
|
||
8 years ago
|
$error = 1;
|
||
|
}
|
||
|
if (not $language)
|
||
|
{
|
||
7 years ago
|
#print $THIS_FILE." ".__LINE__."; Anvil::Tools::Words->key()' called without a language, and 'defaults::languages::output' is not set.\n";
|
||
8 years ago
|
$error = 2;
|
||
|
}
|
||
|
|
||
|
if (not $error)
|
||
|
{
|
||
7 years ago
|
foreach my $this_file (sort {$a cmp $b} keys %{$anvil->data->{words}})
|
||
8 years ago
|
{
|
||
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - this_file: [$this_file], file: [$file]\n";
|
||
|
# If they've specified a file and this doesn't match, skip it.
|
||
|
next if (($file) && ($this_file !~ /$file$/));
|
||
7 years ago
|
if (exists $anvil->data->{words}{$this_file}{language}{$language}{key}{$key}{content})
|
||
8 years ago
|
{
|
||
7 years ago
|
$string = $anvil->data->{words}{$this_file}{language}{$language}{key}{$key}{content};
|
||
8 years ago
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - string: [$string]\n";
|
||
|
last;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - string: [$string]\n";
|
||
|
return($string);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
=head2 language
|
||
|
|
||
|
This sets or returns the output language ISO code.
|
||
|
|
||
|
Get the current log language;
|
||
|
|
||
7 years ago
|
my $language = $anvil->Words->language;
|
||
8 years ago
|
|
||
|
Set the output langauge to Japanese;
|
||
|
|
||
7 years ago
|
$anvil->Words->language({set => "jp"});
|
||
8 years ago
|
|
||
|
=cut
|
||
|
sub language
|
||
|
{
|
||
|
my $self = shift;
|
||
|
my $parameter = shift;
|
||
7 years ago
|
my $anvil = $self->parent;
|
||
8 years ago
|
|
||
|
my $set = defined $parameter->{set} ? $parameter->{set} : "";
|
||
|
|
||
|
if ($set)
|
||
|
{
|
||
|
$self->{WORDS}{LANGUAGE} = $set;
|
||
|
}
|
||
|
|
||
|
if (not $self->{WORDS}{LANGUAGE})
|
||
|
{
|
||
7 years ago
|
$self->{WORDS}{LANGUAGE} = $anvil->data->{defaults}{language}{output};
|
||
8 years ago
|
}
|
||
|
|
||
|
return($self->{WORDS}{LANGUAGE});
|
||
|
}
|
||
|
|
||
8 years ago
|
=head2 read
|
||
|
|
||
|
This reads in a words file containing translated strings used to generated output for the user.
|
||
|
|
||
7 years ago
|
Example to read 'C<< anvil.xml >>';
|
||
8 years ago
|
|
||
7 years ago
|
my $words_file = $anvil->data->{path}{words}{'an-words.xml'};
|
||
|
my $anvil->Words->read({file => $words_file}) or die "Failed to read: [$words_file]. Does the file exist?\n";
|
||
8 years ago
|
|
||
|
Successful read will return '0'. Non-0 is an error;
|
||
|
0 = OK
|
||
|
1 = Invalid file name or path
|
||
|
2 = File not found
|
||
|
3 = File not readable
|
||
8 years ago
|
4 = File found, failed to read for another reason. The error details will be printed.
|
||
8 years ago
|
|
||
7 years ago
|
NOTE: Read works are stored in 'C<< $anvil->data->{words}{<file_name>}{language}{<language>}{string}{content} >>'. Metadata, like what languages are provided, are stored under 'C<< $anvil->data->{words}{<file_name>}{meta}{...} >>'.
|
||
8 years ago
|
|
||
|
Parameters;
|
||
|
|
||
8 years ago
|
=head3 file (required)
|
||
8 years ago
|
|
||
|
This is the file to read.
|
||
|
|
||
|
=cut
|
||
|
sub read
|
||
|
{
|
||
|
my $self = shift;
|
||
|
my $parameter = shift;
|
||
7 years ago
|
my $anvil = $self->parent;
|
||
8 years ago
|
|
||
|
# Setup default values
|
||
|
my $return_code = 0;
|
||
8 years ago
|
my $file = defined $parameter->{file} ? $parameter->{file} : 0;
|
||
7 years ago
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { file => $file }});
|
||
8 years ago
|
|
||
|
if (not $file)
|
||
|
{
|
||
8 years ago
|
# NOTE: Log the problem, do not translate.
|
||
7 years ago
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => "[ Error ] - Words->read()' called without a file name to read."});
|
||
8 years ago
|
$return_code = 1;
|
||
|
}
|
||
|
elsif (not -e $file)
|
||
|
{
|
||
8 years ago
|
# NOTE: Log the problem, do not translate.
|
||
7 years ago
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => "[ Error ] - Words->read()' asked to read: [$file] which was not found."});
|
||
8 years ago
|
$return_code = 2;
|
||
|
}
|
||
|
elsif (not -r $file)
|
||
|
{
|
||
8 years ago
|
# NOTE: Log the problem, do not translate.
|
||
7 years ago
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => "[ Error ] - Words->read()' asked to read: [$file] which was not readable by: [".getpwuid($<)."] (uid/euid: [".$<."])."});
|
||
8 years ago
|
$return_code = 3;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
# Read the file with XML::Simple
|
||
|
my $xml = XML::Simple->new();
|
||
7 years ago
|
eval { $anvil->data->{words}{$file} = $xml->XMLin($file, KeyAttr => { language => 'name', key => 'name' }, ForceArray => [ 'language', 'key' ]) };
|
||
8 years ago
|
if ($@)
|
||
|
{
|
||
|
chomp $@;
|
||
8 years ago
|
my $error = "[ Error ] - The was a problem reading: [$file]. The error was:\n";
|
||
|
$error .= "===========================================================\n";
|
||
|
$error .= $@."\n";
|
||
|
$error .= "===========================================================\n";
|
||
7 years ago
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => $error});
|
||
8 years ago
|
$return_code = 4;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
7 years ago
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0028", variables => { file => $file }});
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
return($return_code);
|
||
|
}
|
||
|
|
||
8 years ago
|
=head2 string
|
||
|
|
||
|
This method takes a string key and returns the string in the requested language. If not key is passed, the language key in 'defaults::languages::output' is used. A hash reference containing variables can be provided to inject values into a string.
|
||
|
|
||
|
If the requested string is not found, 'C<< #!not_found!# >>' is returned.
|
||
|
|
||
|
Example to retrieve 'C<< t_0001 >>';
|
||
|
|
||
7 years ago
|
my $string = $anvil->Words->string({key => 't_0001'});
|
||
8 years ago
|
|
||
|
This time, requesting 'C<< t_0002 >>' and passing in two variables. Note that 'C<< t_0002 >>' in Canadian English is;
|
||
|
|
||
|
Test Out of order: [#!variable!second!#] replace: [#!variable!first!#].
|
||
|
|
||
|
So to request this string in Canadian English is the two variables inserted, we would call:
|
||
|
|
||
7 years ago
|
my $string = $anvil->Words->string({
|
||
8 years ago
|
language => 'en_CA',
|
||
|
key => 't_0002',
|
||
|
variables => {
|
||
8 years ago
|
first => "foo",
|
||
|
second => "bar",
|
||
8 years ago
|
},
|
||
|
});
|
||
|
|
||
|
This would return;
|
||
|
|
||
|
Test Out of order: [bar] replace: [foo].
|
||
|
|
||
|
Normally, there should never be a key collision. However, just in case you find yourself needing to request the string from a specific file, you can do the same call with a file specified.
|
||
|
|
||
7 years ago
|
my $string = $anvil->Words->string({
|
||
8 years ago
|
language => 'en_CA',
|
||
7 years ago
|
file => 'anvil.xml',
|
||
8 years ago
|
key => 't_0002',
|
||
|
variables => {
|
||
|
first => "foo",
|
||
|
second => "bar",
|
||
|
},
|
||
|
});
|
||
|
|
||
|
If the passed in key isn't found (at all, or for the given language or file if specified), then 'C<< #!not_found!# >>' will be returned.
|
||
|
|
||
|
Parameters;
|
||
|
|
||
|
=head3 file (optional)
|
||
|
|
||
|
This is the specific file to read the string from. It should generally not be needed as string keys should not be reused. However, if it happens, this is a way to specify which file's version you want.
|
||
|
|
||
|
=head3 key (required)
|
||
|
|
||
|
This is the key to return the string for.
|
||
|
|
||
8 years ago
|
NOTE: This is ignored when 'C<< string >>' is used.
|
||
|
|
||
8 years ago
|
=head3 language (optional)
|
||
|
|
||
|
This is the ISO code for the language you wish to read the string from. For example, 'en_CA' to get the Canadian English string, or 'jp' for the Japanese string.
|
||
|
|
||
|
When no language is passed, 'C<< defaults::languages::output >>' is used.
|
||
|
|
||
8 years ago
|
=head3 string (optional)
|
||
|
|
||
|
If this is passed, it is treated as a raw string that needs variables inserted. When this is used, the 'C<< key >>' parameter is ignored.
|
||
|
|
||
8 years ago
|
=head3 variables (depends)
|
||
|
|
||
|
If the string being requested has one or more 'C<< #!variable!x!# >>' replacement keys, then you must pass a hash reference containing the keys / value pairs where the key matches the replacement string.
|
||
|
|
||
|
=cut
|
||
|
sub string
|
||
|
{
|
||
|
my $self = shift;
|
||
|
my $parameter = shift;
|
||
7 years ago
|
my $anvil = $self->parent;
|
||
8 years ago
|
|
||
|
# Setup default values
|
||
|
my $key = defined $parameter->{key} ? $parameter->{key} : "";
|
||
7 years ago
|
my $language = defined $parameter->{language} ? $parameter->{language} : $anvil->Words->language;
|
||
8 years ago
|
my $file = defined $parameter->{file} ? $parameter->{file} : "";
|
||
8 years ago
|
my $string = defined $parameter->{string} ? $parameter->{string} : "";
|
||
8 years ago
|
my $variables = defined $parameter->{variables} ? $parameter->{variables} : "";
|
||
|
|
||
8 years ago
|
# If we weren't passed a raw string, we'll get the string from our ->key() method, the inject any
|
||
|
# variables, if needed. This also handles the initial sanity checks. If we get back '#!not_found!#',
|
||
|
# we'll exit.
|
||
|
if (not $string)
|
||
|
{
|
||
7 years ago
|
$string = $anvil->Words->key({
|
||
8 years ago
|
key => $key,
|
||
|
language => $language,
|
||
|
file => $file,
|
||
|
});
|
||
|
}
|
||
8 years ago
|
|
||
|
if (($string ne "#!not_found!#") && ($string =~ /#!([^\s]+?)!#/))
|
||
|
{
|
||
|
# We've got a string and variables from the caller, so inject them as needed.
|
||
|
my $loops = 0;
|
||
7 years ago
|
my $limit = $anvil->data->{defaults}{limits}{string_loops} =~ /^\d+$/ ? $anvil->data->{defaults}{limits}{string_loops} : 1000;
|
||
8 years ago
|
|
||
8 years ago
|
# If the user didn't pass in any variables, then we're in trouble.
|
||
|
if (($string =~ /#!variable!(.+?)!#/s) && ((not $variables) or (ref($variables) ne "HASH")))
|
||
|
{
|
||
|
# Escape the variables before the sending the error
|
||
|
while ($string =~ /#!variable!(.+?)!#/s)
|
||
|
{
|
||
|
$string =~ s/#!variable!(.*?)!#/!!variable!$1!!/s;
|
||
|
|
||
|
# Die if I've looped too many times.
|
||
|
$loops++;
|
||
|
die "$THIS_FILE ".__LINE__."; Infinite loop detected while processing the string: [".$string."] from the key: [$key] in language: [$language], exiting.\n" if $loops > $limit;
|
||
|
}
|
||
7 years ago
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0042", variables => { string => $string }});
|
||
8 years ago
|
return("#!error!#");
|
||
|
}
|
||
|
|
||
8 years ago
|
# We set the 'loop' variable to '1' and check it at the end of each pass. This is done
|
||
|
# because we might inject a string near the end that adds a replacement key to an
|
||
|
# otherwise-processed string and we don't want to miss that.
|
||
|
my $loop = 1;
|
||
|
while ($loop)
|
||
|
{
|
||
|
# First, look for any '#!...!#' keys that we don't recognize and protect them. We'll
|
||
|
# restore them once we're out of this loop.
|
||
|
foreach my $check ($string =~ /#!([^\s]+?)!#/)
|
||
|
{
|
||
|
if (($check !~ /^replace/) &&
|
||
|
($check !~ /^data/) &&
|
||
|
($check !~ /^string/) &&
|
||
|
($check !~ /^variable/))
|
||
|
{
|
||
|
# Simply invert the '#!...!#' to '!#...#!'.
|
||
|
$string =~ s/#!($check)!#/!#$1#!/g;
|
||
|
}
|
||
|
|
||
|
# Die if I've looped too many times.
|
||
|
$loops++;
|
||
|
die "$THIS_FILE ".__LINE__."; Infinite loop detected while processing the string: [".$string."] from the key: [$key] in language: [$language], exiting.\n" if $loops > $limit;
|
||
|
}
|
||
|
|
||
|
# Now, look for any '#!string!x!#' embedded strings.
|
||
|
while ($string =~ /#!string!(.+?)!#/)
|
||
|
{
|
||
|
my $key = $1;
|
||
7 years ago
|
my $this_string = $anvil->Words->key({
|
||
8 years ago
|
key => $key,
|
||
|
language => $language,
|
||
|
file => $file,
|
||
|
});
|
||
|
if ($this_string eq "#!not_found!#")
|
||
|
{
|
||
|
# The key was bad...
|
||
|
$string =~ s/#!string!$key!#/!!e[$key]!!/;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$string =~ s/#!string!$key!#/$this_string/;
|
||
|
}
|
||
|
|
||
|
# Die if I've looped too many times.
|
||
|
$loops++;
|
||
|
die "$THIS_FILE ".__LINE__."; Infinite loop detected while processing the string: [".$string."] from the key: [$key] in language: [$language], exiting.\n" if $loops > $limit;
|
||
|
}
|
||
|
|
||
|
# Now insert variables in the strings.
|
||
|
while ($string =~ /#!variable!(.+?)!#/s)
|
||
|
{
|
||
|
my $variable = $1;
|
||
|
|
||
|
# Sometimes, #!variable!*!# is used in explaining things to users. So we need
|
||
|
# to escape it. It will be restored later in '_restore_protected()'.
|
||
|
if ($variable eq "*")
|
||
|
{
|
||
|
$string =~ s/#!variable!\*!#/!#variable!*#!/;
|
||
|
next;
|
||
|
}
|
||
8 years ago
|
if ($variable eq "")
|
||
|
{
|
||
|
$string =~ s/#!variable!\*!#/!#variable!#!/;
|
||
|
next;
|
||
|
}
|
||
8 years ago
|
|
||
|
if (not defined $variables->{$variable})
|
||
|
{
|
||
|
# I can't expect there to always be a defined value in the variables
|
||
|
# array at any given position so if it is blank qw blank the key.
|
||
|
$string =~ s/#!variable!$variable!#//;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
my $value = $variables->{$variable};
|
||
|
chomp $value;
|
||
|
$string =~ s/#!variable!$variable!#/$value/;
|
||
|
}
|
||
|
|
||
|
# Die if I've looped too many times.
|
||
|
$loops++;
|
||
|
die "$THIS_FILE ".__LINE__."; Infinite loop detected while processing the string: [".$string."] from the key: [$key] in language: [$language], exiting.\n" if $loops > $limit;
|
||
|
}
|
||
|
|
||
7 years ago
|
# Next, convert '#!data!x!#' to the value in '$anvil->data->{x}'.
|
||
8 years ago
|
while ($string =~ /#!data!(.+?)!#/)
|
||
|
{
|
||
|
my $id = $1;
|
||
|
if ($id =~ /::/)
|
||
|
{
|
||
|
# Multi-dimensional hash.
|
||
7 years ago
|
my $value = $anvil->_get_hash_reference({ key => $id });
|
||
8 years ago
|
if (not defined $value)
|
||
|
{
|
||
|
$string =~ s/#!data!$id!#/!!a[$id]!!/;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$string =~ s/#!data!$id!#/$value/;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
# One dimension
|
||
7 years ago
|
if (not defined $anvil->data->{$id})
|
||
8 years ago
|
{
|
||
|
$string =~ s/#!data!$id!#/!!b[$id]!!/;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
7 years ago
|
my $value = $anvil->data->{$id};
|
||
8 years ago
|
$string =~ s/#!data!$id!#/$value/;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Die if I've looped too many times.
|
||
|
$loops++;
|
||
|
die "$THIS_FILE ".__LINE__."; Infinite loop detected while processing the string: [".$string."] from the key: [$key] in language: [$language], exiting.\n" if $loops > $limit;
|
||
|
}
|
||
|
|
||
|
$loops++;
|
||
|
die "$THIS_FILE ".__LINE__."; Infinite loop detected while processing the string: [".$string."] from the key: [$key] in language: [$language], exiting.\n" if $loops > $limit;
|
||
|
|
||
|
# If there are no replacement keys left, exit the loop.
|
||
|
if ($string !~ /#!([^\s]+?)!#/)
|
||
|
{
|
||
|
$loop = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Restore any protected keys. Reset the loop counter, too.
|
||
|
$loops = 0;
|
||
|
$loop = 1;
|
||
|
while ($loop)
|
||
|
{
|
||
|
$string =~ s/!#([^\s]+?)#!/#!$1!#/g;
|
||
|
|
||
|
$loops++;
|
||
|
die "$THIS_FILE ".__LINE__."; Infinite loop detected while processing the string: [".$string."] from the key: [$key] in language: [$language], exiting.\n" if $loops > $limit;
|
||
|
|
||
|
if ($string !~ /!#[^\s]+?#!/)
|
||
|
{
|
||
|
$loop = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
# In some multi-line strings, the last line will be '\t\t</key>'. We clean this up.
|
||
|
$string =~ s/\t\t$//;
|
||
|
|
||
8 years ago
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - string: [$string]\n";
|
||
|
return($string);
|
||
|
}
|
||
|
|
||
8 years ago
|
# =head3
|
||
|
#
|
||
|
# Private Functions;
|
||
|
#
|
||
|
# =cut
|
||
|
|
||
|
#############################################################################################################
|
||
|
# Private functions #
|
||
|
#############################################################################################################
|
||
|
|
||
|
1;
|