[Rt-commit] r4506 - in Text-Tags: . lib/Text/Tags t

alexmv at bestpractical.com alexmv at bestpractical.com
Thu Feb 9 01:32:13 EST 2006


Author: alexmv
Date: Thu Feb  9 01:32:13 2006
New Revision: 4506

Added:
   Text-Tags/t/03.quoted_join.t
Modified:
   Text-Tags/   (props changed)
   Text-Tags/MANIFEST
   Text-Tags/lib/Text/Tags.pm
   Text-Tags/lib/Text/Tags/Parser.pm

Log:
 r8993 at zoq-fot-pik:  chmrr | 2006-02-09 01:31:24 -0500
  * Added join_quoted_tags method, which forces quoting around tags
  * Version bump to 0.03
  * Wrap POD


Modified: Text-Tags/MANIFEST
==============================================================================
--- Text-Tags/MANIFEST	(original)
+++ Text-Tags/MANIFEST	Thu Feb  9 01:32:13 2006
@@ -16,5 +16,6 @@
 t/00.load.t
 t/01.parse.t
 t/02.join.t
+t/03.quoted_join.t
 t/pod-coverage.t
 t/pod.t

Modified: Text-Tags/lib/Text/Tags.pm
==============================================================================
--- Text-Tags/lib/Text/Tags.pm	(original)
+++ Text-Tags/lib/Text/Tags.pm	Thu Feb  9 01:32:13 2006
@@ -1,6 +1,6 @@
 package Text::Tags;
 
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 use warnings;
 use strict;

Modified: Text-Tags/lib/Text/Tags/Parser.pm
==============================================================================
--- Text-Tags/lib/Text/Tags/Parser.pm	(original)
+++ Text-Tags/lib/Text/Tags/Parser.pm	Thu Feb  9 01:32:13 2006
@@ -46,6 +46,19 @@
 sub join_tags {
     my $self = shift;
     my @tags = @_;
+    return $self->_join_tags(undef, @tags);
+}
+
+sub join_quoted_tags {
+    my $self = shift;
+    my @tags = @_;
+    return $self->_join_tags(1, @tags);
+}
+
+sub _join_tags {
+    my $self = shift;
+    my $always_quote = shift;
+    my @tags = @_;
 
     my %seen;
     my @quoted_tags;
@@ -69,7 +82,7 @@
 
             # It contains a ", so either it needs to be unquoted or
             # single-quoted
-            if ( $tag =~ / / or $tag =~ /,/ or $tag =~ /^"/ ) {
+            if ( $tag =~ / / or $tag =~ /,/ or $tag =~ /^"/ or $always_quote) {
                 $quote = q{'};
             } else {
                 $quote = q{};
@@ -78,12 +91,12 @@
 
             # It contains a ', so either it needs to be unquoted or
             # double-quoted
-            if ( $tag =~ / / or $tag =~ /,/ or $tag =~ /^'/ ) {
+            if ( $tag =~ / / or $tag =~ /,/ or $tag =~ /^'/ or $always_quote) {
                 $quote = q{"};
             } else {
                 $quote = q{};
             }
-        } elsif ( $tag =~ /[ ,]/ ) {
+        } elsif ( $tag =~ /[ ,]/ or $always_quote) {
 
             # By this point we know that it contains no quotes.
             # But it needs to be quoted.
@@ -120,28 +133,31 @@
   
 =head1 DESCRIPTION
 
-Parses "folksonomies", which are simple space-or-comma-separated-but-optionally-quoted tag lists.
+Parses "folksonomies", which are simple
+space-or-comma-separated-but-optionally-quoted tag lists.
 
-Specifically, tags can be any string, as long as they don't contain both a
-single and a double quote.  Hopefully, this is a pretty obscure restriction.  In
-addition, all whitespace inside tags is normalized to a single space (with no
-leading or trailing whitespace).  
-
-In a tag list string, tags can optionally be quoted with either single or double
-quotes.  B<There is no escaping of either kind of quote>, although you can
-include one type of quote inside a string quoted with the other.  Quotes can
-also just be included inside tags, as long as they aren't at the beginning; thus
-a tag like C<joe's> can just be entered without any extra quoting.  Tags are
-separated by whitespace and/or commas, though quoted tags can run into each
-other without whitespace.  Empty tags (put in explicitly with C<""> or C<''>)
-are ignored.  (Note that commas are not normalized with whitespace, and can be
-included in a tag if you quote them.)
-
-Why did the previous paragraph need to be so detailed?  Because L<Text::Tags::Parser> 
-B<always successfully parses> every line.  That is, every single tags line converts into
-a list of tags, without any error conditions.  For general use, you can just understand the
-rules as being B<separate tags with spaces or commas, and put either kind of quotes around tags that
-need to have spaces>.
+Specifically, tags can be any string, as long as they don't contain
+both a single and a double quote.  Hopefully, this is a pretty obscure
+restriction.  In addition, all whitespace inside tags is normalized to
+a single space (with no leading or trailing whitespace).
+
+In a tag list string, tags can optionally be quoted with either single
+or double quotes.  B<There is no escaping of either kind of quote>,
+although you can include one type of quote inside a string quoted with
+the other.  Quotes can also just be included inside tags, as long as
+they aren't at the beginning; thus a tag like C<joe's> can just be
+entered without any extra quoting.  Tags are separated by whitespace
+and/or commas, though quoted tags can run into each other without
+whitespace.  Empty tags (put in explicitly with C<""> or C<''>) are
+ignored.  (Note that commas are not normalized with whitespace, and
+can be included in a tag if you quote them.)
+
+Why did the previous paragraph need to be so detailed?  Because
+L<Text::Tags::Parser> B<always successfully parses> every line.  That
+is, every single tags line converts into a list of tags, without any
+error conditions.  For general use, you can just understand the rules
+as being B<separate tags with spaces or commas, and put either kind of
+quotes around tags that need to have spaces>.
 
 =head1 METHODS
 
@@ -149,20 +165,28 @@
 
 =item B<new>
 
-Creates a new L<Text::Tags::Parser> object.  In this version of the module, the objects
-do not actually hold any state, but this could change in a future version.
+Creates a new L<Text::Tags::Parser> object.  In this version of the
+module, the objects do not actually hold any state, but this could
+change in a future version.
 
 =item B<parse_tags>($string)
 
-Given a tag list string, returns a list of tags (unquoted) using the rules described 
-above.
-Any given tag will show up at most once in the output list.
+Given a tag list string, returns a list of tags (unquoted) using the
+rules described above.  Any given tag will show up at most once in the
+output list.
 
 =item B<join_tags>(@tags)
 
-Given a list of tags, returns a tag list string containing them (appropriately quoted).
-Note that illegal tags will have all of their double quotes converted to single quotes.
-Any given tag will show up at most once in the output string.
+Given a list of tags, returns a tag list string containing them
+(appropriately quoted).  Note that illegal tags will have all of their
+double quotes converted to single quotes.  Any given tag will show up
+at most once in the output string.
+
+=item B<join_quoted_tags>(@tags)
+
+As L</join_tags>, but every tag will be delimited by wither single or
+double quotes -- unlike L</join_tags>, which only quotes when
+necessary.
 
 =back
 
@@ -181,23 +205,23 @@
 
 =head1 SEE ALSO
 
-L<Text::Folksonomies>, a module with similar functionality
-but has much more simplistic quote handling.  (Specifically, it doesn't
-allow you to put any type of quote into a tag.)  But if you don't care
-about that sort of support, it seems to work fine.
+L<Text::Folksonomies>, a module with similar functionality but has
+much more simplistic quote handling.  (Specifically, it doesn't allow
+you to put any type of quote into a tag.)  But if you don't care about
+that sort of support, it seems to work fine.
 
 
 =head1 AUTHOR
 
 David Glasser  C<< <glasser at bestpractical.com> >>
 
-
 =head1 LICENCE AND COPYRIGHT
 
-Copyright (c) 2005, Best Practical Solutions, LLC.  All rights reserved.
+Copyright (c) 2005, Best Practical Solutions, LLC.  All rights
+reserved.
 
-This module is free software; you can redistribute it and/or
-modify it under the same terms as Perl itself. See L<perlartistic>.
+This module is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself. See L<perlartistic>.
 
 
 =head1 DISCLAIMER OF WARRANTY

Added: Text-Tags/t/03.quoted_join.t
==============================================================================
--- (empty file)
+++ Text-Tags/t/03.quoted_join.t	Thu Feb  9 01:32:13 2006
@@ -0,0 +1,19 @@
+use Test::More tests => 14;
+
+BEGIN { use_ok 'Text::Tags::Parser' }
+
+my $parser = Text::Tags::Parser->new;
+isa_ok($parser, 'Text::Tags::Parser');
+
+is($parser->join_quoted_tags(), q{});
+is($parser->join_quoted_tags(qw/foo bar baz/), q{"foo" "bar" "baz"});
+is($parser->join_quoted_tags(qw/foo bar baz bar/), q{"foo" "bar" "baz"});
+is($parser->join_quoted_tags(qw/foo bar's baz /), q{"foo" "bar's" "baz"});
+is($parser->join_quoted_tags('foo', 'foo   bar'), q{"foo" "foo bar"});
+is($parser->join_quoted_tags('foo', 'fo"o   bar'), q{"foo" 'fo"o bar'});
+is($parser->join_quoted_tags('beep', 'fo"r'), q{"beep" 'fo"r'});
+is($parser->join_quoted_tags(q{"Foo's"}), q{"'Foo's'"});
+is($parser->join_quoted_tags(q{Bob "Foo's"}), q{"Bob 'Foo's'"});
+is($parser->join_quoted_tags(q{a'b"c}, 'bla'), q{"a'b'c" "bla"});
+is($parser->join_quoted_tags(q{ab"c  bah}, 'bla'), q{'ab"c bah' "bla"});
+is($parser->join_quoted_tags(q{ab'c  bah}, 'bla'), q{"ab'c bah" "bla"});


More information about the Rt-commit mailing list