NAME
Locale::Maketext::Utils::Phrase::Norm - Normalize and perform lint-like analysis of phrases
VERSION
This document describes Locale::Maketext::Utils::Phrase::Norm version 0.2
SYNOPSIS
use Locale::Maketext::Utils::Phrase::Norm;
my $norm = Locale::Maketext::Utils::Phrase::Norm->new_source() || die;
my $result = $norm->normalize('This office has worked [quant,_1,day,days,zero days] without an “accident”.');
# process $result
DESCRIPTION
Analyze, report, and normalize a maketext style phrase based on rules organized into filter modules.
INTERFACE
Main object
new_source()
A “source phrase” is a phrase (suitable for a call to maketext()) in the main locale’s language that we intend to be localizable.
Typically this is the key of a lexicon’s hash but it can be the value if the main locale lexicon’s key is an “Arbitrary Key”, that is, if the value is different from the key in the main locale’s lexicon.
new_source() creates a new object with all the filters initialized for source phrases.
Giving no arguments means it will employ all of the default filter modules (documented in "DEFAULT FILTERS").
Otherwise the optional arguments are:
- A list of filter module name spaces to run after the default filter modules.
-
If the given module name does not contain any package seperators it will be treated as if it needs prepended with 'Locale::Maketext::Utils::Phrase::Norm::'.
e.g. Given 'Locale::Maketext::Utils::Phrase::Norm::MyCoolFilter' you can pass the name 'MyCoolFilter'.
- The last argument can be a hashref of options:
-
my $norm = Locale::Maketext::Utils::Phrase::Norm->new_source('My::Filter::XYZ'); # all default filters followed by the My::Filter::XYZ filter my $norm = Locale::Maketext::Utils::Phrase::Norm->new_source('My::Filter::XYZ', { 'skip_defaults_when_given_filters' => 1 }); # only do My::Filter::XYZ the filter
The options are outlined below and are all optional:
- 'skip_defaults_when_given_filters'
-
Boolean.
When false (default) your filters are appended to the list of default filters.
When true the default list of filters is not used.
- 'maketext_object'
-
An object that can be used by filters should they need one to perform their task. Currently, it must have a makethis() method.
The main object and filter each have a get_maketext_object() method to fetch this when needed.
If you did not specify an argument here get_maketext_object() returns a Locale::Maketext::Utils::Mock object. That means all the cool stuff in your locale object that you might want to use in your filter will not be available.
- 'run_extra_filters'
-
Boolean.
When false (default) "extra filters" are not executed.
When true the "extra filters" are executed.
- 'exclude_filters'
-
A hashref of filters to exclude from the object regardless of the list in effect.
The key can be the long or short name space of the filter modules and the value must be true for the entry to take effect.
new_source() carp()s and returns false if there is some sort of failure (documented in "DIAGNOSTICS").
new_target()
A “target phrase” is the translated version of a given source phrase. This is the value of a non-main-locale lexicon’s hash.
new_target() is just like new_source() but uses a subset of the "DEFAULT FILTERS" that apply to translations.
Currently the exclusion of BeginUpper and EndPunc from the "DEFAULT FILTERS" is what makes up this object.
normalize()
Takes a phrase as the only argument and returns a result object (documented in "Result Object").
delete_cache()
The result of normalize() is cached internally so calling it subsequent times with the same string won’t result in it being reprocessed.
This method deletes the internal cache. Returns the hashref that was removed.
get_maketext_object()
Returns the object you instantiated the "Main object" with.
If you did not specify an argument a Locale::Maketext::Utils::Mock object is used. That means all the cool stuff in your locale object that you might want to use in your filter will not be available.
set_maketext_object()
Takes the same object you’d pass to the constructor method via ‘maketext_object’.
This is what will be used on subsequent calls to normalize().
run_extra_filters()
Boolean return value of if we are running "extra filters" or not.
enable_extra_filters()
No arguments, enables the running of any "extra filters" on subsequent calls to normalize().
disable_extra_filters()
No arguments, disables the running of any "extra filters" on subsequent calls to normalize().
Result Object
get_status()
Returns the status of all the filters:
- True means no violations
- -1 (i.e. still true) means there were warnings but no violations.
- False means there was at least one violation and possibly warnings.
get_warning_count()
Return the number of warnings from all filters combined.
get_violation_count()
Return the number of violations from all filters combined.
get_filter_results()
Return an array ref of filter result objects (documented in "Filter Result Object").
get_orig_str()
Get the phrase as passed in before any modifications by filters.
get_aggregate_result()
Get the phrase after all filters had a chance to modify it.
filters_modify_string()
Returns true if any of the filters resulted in a string different from what you passed it. False otherwise.
Filter Result Object
Intended for use in a filter module.
See "ANATOMY OF A FILTER MODULE" for more info.
add_violation()
Add a violation.
add_warning()
Add a warning.
get_string_sr()
Returns a SCALAR reference to the modified version of the string that the filter can use to modify the string.
return_value()
returns an array of the status, violation count, warning count, and filter_modifies_string().
It is what the filter’s normalize_maketext_string() should return;
get_maketext_object()
Returns the object you instantiated the "Main object" with.
If you did not specify an argument a Locale::Maketext::Utils::Mock object is used. That means all the cool stuff in your locale object that you might want to use in your filter will not be available.
run_extra_filters()
Returns a boolean value of if we are running extra filters or not.
if ( $filter->run_extra_filters() ) {
# do extra check for violations/warnings here
}
You can use this to check if the filter should run certain tests or not. You can even skip an entire filter by use of return_value_noop().
return_value_noop()
Get an appropriate return_value() for when the entire filter falls under the category of "extra filters".
return $filter->return_value_noop() if !$filter->run_extra_filters();
Intended for use when processing results.
These can be used in a filter module’s filter code if you find use for them there. See "ANATOMY OF A FILTER MODULE" for more info.
get_status()
Returns the status of the filter:
- True means no violations
- -1 (i.e. still true) means there were warnings but no violations.
- False means there was at least one violation and possibly warnings.
get_package()
Get the current filter’s package.
get_orig_str()
Get the phrase as passed in before any modifications by the filter.
get_new_str()
Get the phrase after the filter had a chance to modify it.
get_violations()
Return an array ref of violations added via add_violation().
If there are no violations it returns false.
get_warnings()
Return an array ref of violations added via add_warning().
If there are no warnings it returns false.
get_warning_count()
Returns the number of warnings the filter resulted in.
get_violation_count()
Returns the number of violations the filter resulted in.
filter_modifies_string()
Returns true if the filter resulted in a string different from what you passed it. False otherwise.
DEFAULT FILTERS
The included default filters are listed below in the order they are executed by default.
- NonBytesStr
- WhiteSpace
- Grapheme
- Ampersand
- Markup
- Ellipsis
- BeginUpper
- EndPunc
- Consider
- Escapes
- Compiles
extra filters
It may be desireable for some filters to not run by default but still be easily applied when needed.
The extra filter mechanism allows for this as documented specifically throught this POD.
No filters fall under "extra filters" currently.
ANATOMY OF A FILTER MODULE
A filter module is simply a package that defines a function that does the filtering of the phrase.
normalize_maketext_string()
This gets passed a single argument: the "Filter Result Object" that defines data about the phrase.
That object can be used to do the actual checks, modifications if any, and return the expected info back (via $filter->return_value).
package My::Phrase::Filter::X;
sub normalize_maketext_string {
my ($filter) = @_;
my $string_sr = $filter->get_string_sr();
if (${$string_sr} =~ s/X/[comment,unexpected X]/g) {
$filter->add_warning('X might be invalid might wanna check that');
# or
# $filter->add_violation('Text of violation here');
}
return $filter->return_value;
}
1;
It’s a good idea to explain the filter in it’s POD. Check out _Stub for some boilerplate.
DIAGNOSTICS
%s does not implement normalize_maketext_string()
-
The constructor method was able to load the filter %s but that class does not have a normalize_maketext_string() method.
Can't locate %s.pm in @INC …
-
The constructor method was not able to load the filter %s, the actual error comes from perl via $@ from Module::Want
Filter list is empty!
-
After all initialization and no other errors the list of filters is somehow empty.
Given maketext object does not have a makethis() method.
-
The value of the maketext_object key you passed to the constructor method or the value passed to set_maketext_object() does not define a makethis() method.
Given maketext object is not a reference.
-
The value of the maketext_object key you passed to the constructor method or the value passed to set_maketext_object() is not an object.
new() is deprecated, use new_source() instead
-
Your code uses the deprecated constructor and needs to be updated.
You must pass a value to normalize()
-
Your code called normalize() without giving it a value to, well, normalize.
CONFIGURATION AND ENVIRONMENT
Locale::Maketext::Utils::Phrase::Norm requires no configuration files or environment variables.
DEPENDENCIES
Module::Want, Encode (for the WhiteSpace filter)
INCOMPATIBILITIES
None reported.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to bug-locale-maketext-utils@rt.cpan.org
, or through the web interface at http://rt.cpan.org.
SEE ALSO
Locale::Maketext::Utils::Phrase::cPanel
AUTHOR
Daniel Muey <http://drmuey.com/cpan_contact.pl>
LICENCE AND COPYRIGHT
Copyright (c) 2012 cPanel, Inc. <copyright@cpanel.net>>
. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.
DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.