~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Plurals.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
=begin TML
 
3
 
 
4
---+ package Foswiki::Plurals
 
5
 
 
6
Handle conversion of plural topic names to singular form.
 
7
 
 
8
=cut
 
9
 
 
10
package Foswiki::Plurals;
 
11
 
 
12
use strict;
 
13
 
 
14
=begin TML
 
15
 
 
16
---++ StaticMethod singularForm($web, $pluralForm) -> $singularForm
 
17
 
 
18
Try to singularise plural topic name.
 
19
   * =$web= - the web the topic must be in
 
20
   * =$pluralForm= - topic name
 
21
Returns undef if no singular form exists, otherwise returns the
 
22
singular form of the topic
 
23
 
 
24
I18N - Only apply plural processing if site language is English, or
 
25
if a built-in English-language web.  Plurals
 
26
apply to names ending in 's', where topic doesn't exist with plural
 
27
name.
 
28
 
 
29
Note that this is highly langauge specific, and need to be enabled
 
30
on a per-installation basis with $Foswiki::cfg{PluralToSingular}.
 
31
 
 
32
=cut
 
33
 
 
34
sub singularForm {
 
35
    my ( $web, $pluralForm ) = @_;
 
36
    $web =~ s#\.#/#go;
 
37
 
 
38
    # Plural processing only if enabled in configure or one of the
 
39
    # distributed webs
 
40
    return undef
 
41
      unless ( $Foswiki::cfg{PluralToSingular}
 
42
        or $web eq $Foswiki::cfg{UsersWebName}
 
43
        or $web eq $Foswiki::cfg{SystemWebName} );
 
44
    return undef unless ( $pluralForm =~ /s$/ );
 
45
 
 
46
    # Topic name is plural in form
 
47
    my $singularForm = $pluralForm;
 
48
    $singularForm =~ s/ies$/y/;          # plurals like policy / policies
 
49
    $singularForm =~ s/sses$/ss/;        # plurals like address / addresses
 
50
    $singularForm =~ s/ches$/ch/;        # plurals like search / searches
 
51
    $singularForm =~ s/(oes|os)$/o/;     # plurals like veto / vetoes
 
52
    $singularForm =~ s/([Xx])es$/$1/;    # plurals like box / boxes
 
53
    $singularForm =~ s/([^s])s$/$1/;     # others, excluding ss like address(es)
 
54
    return $singularForm;
 
55
}
 
56
 
 
57
1;
 
58
__DATA__
 
59
# Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
60
#
 
61
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
 
62
# Foswiki Contributors are listed in the AUTHORS file in the root of
 
63
# this distribution.
 
64
# NOTE: Please extend that file, not this notice.
 
65
#
 
66
# Additional copyrights apply to some or all of the code in this
 
67
# file as follows:
 
68
# Copyright (C) 2005 Martin Cleaver.
 
69
# Copyright (C) 1999-2007 TWiki Contributors. All Rights Reserved.
 
70
# TWiki Contributors are listed in the AUTHORS file in the root of
 
71
# this distribution.
 
72
#
 
73
# This program is free software; you can redistribute it and/or
 
74
# modify it under the terms of the GNU General Public License
 
75
# as published by the Free Software Foundation; either version 2
 
76
# of the License, or (at your option) any later version. For
 
77
# more details read LICENSE in the root of this distribution.
 
78
#
 
79
# This program is distributed in the hope that it will be useful,
 
80
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
81
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
82
#
 
83
# As per the GPL, removal of this notice is prohibited.