The Pug Automatic: Flickr Style Tagging in Rails
Someone asked on IRC for Ruby code to split tags Flickr style, e.g. getting the tags fromtag1 tag2 'tag 3 has spaces' tag4.I came up with this:
def parse_tags(string)   string.split(/"(.+?)"|\s+/).reject {|s| s.empty? } endIt even preserves tag order, which you wouldn't get if you'd firstgsubout (and store) quoted tags and thensplitthe rest."
I haven't tried it yet but looking at it looks like a fine implementation. Very simple.
[Via Pug Automatic.]









On Wednesday, Mar 5
mac:~ sam$ irb
>> def parse_tags(string)
>> string.split(/"(.+?)"|s+/).reject {|s| s.empty? }
>> end
=> nil
?> parse_tags("foo bar zooma groober pagerank")
=> ["foo bar zooma groober pagerank"]
Doens't look like it works to me. It returns an array with a single element(string)
On Wednesday, Mar 5
There is a backslash missing in front of the first s:
def parse_tags(string)
string.split(/"(.+?)"|\s+/).reject {|s| s.empty? }
end
Anyway, tags with spaces must be quoted with double quotes, not single quotes as in the article
>> def parse_tags(string)
>> string.split(/"(.+?)"|\s+/).reject {|s| s.empty?
>> end
=> nil
>> parse_tags('foo bar "zooma groober" pagerank')
=> ["foo", "bar", "zooma groober", "pagerank"]
On Wednesday, Mar 5
Christian is right. It's getting stripped in the editor. Will fix that.