-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #579 from ProGM/implement-custom-filters
Adding additional configurable filters to tags
- Loading branch information
Showing
12 changed files
with
260 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module ActsAsTaggableOn | ||
## | ||
# Returns a new TagList using the given tag string. | ||
# | ||
# Example: | ||
# tag_list = ActsAsTaggableOn::DefaultParser.parse("One , Two, Three") | ||
# tag_list # ["One", "Two", "Three"] | ||
class DefaultParser < GenericParser | ||
|
||
def parse | ||
string = @tag_list | ||
|
||
string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join) | ||
TagList.new.tap do |tag_list| | ||
string = string.to_s.dup | ||
|
||
string.gsub!(double_quote_pattern) { | ||
# Append the matched tag to the tag list | ||
tag_list << Regexp.last_match[2] | ||
# Return the matched delimiter ($3) to replace the matched items | ||
'' | ||
} | ||
|
||
string.gsub!(single_quote_pattern) { | ||
# Append the matched tag ($2) to the tag list | ||
tag_list << Regexp.last_match[2] | ||
# Return an empty string to replace the matched items | ||
'' | ||
} | ||
|
||
# split the string by the delimiter | ||
# and add to the tag_list | ||
tag_list.add(string.split(Regexp.new delimiter)) | ||
end | ||
end | ||
|
||
|
||
# private | ||
def delimiter | ||
# Parse the quoted tags | ||
d = ActsAsTaggableOn.delimiter | ||
# Separate multiple delimiters by bitwise operator | ||
d = d.join('|') if d.kind_of?(Array) | ||
d | ||
end | ||
|
||
# ( # Tag start delimiter ($1) | ||
# \A | # Either string start or | ||
# #{delimiter} # a delimiter | ||
# ) | ||
# \s*" # quote (") optionally preceded by whitespace | ||
# (.*?) # Tag ($2) | ||
# "\s* # quote (") optionally followed by whitespace | ||
# (?= # Tag end delimiter (not consumed; is zero-length lookahead) | ||
# #{delimiter}\s* | # Either a delimiter optionally followed by whitespace or | ||
# \z # string end | ||
# ) | ||
def double_quote_pattern | ||
/(\A|#{delimiter})\s*"(.*?)"\s*(?=#{delimiter}\s*|\z)/ | ||
end | ||
|
||
# ( # Tag start delimiter ($1) | ||
# \A | # Either string start or | ||
# #{delimiter} # a delimiter | ||
# ) | ||
# \s*' # quote (') optionally preceded by whitespace | ||
# (.*?) # Tag ($2) | ||
# '\s* # quote (') optionally followed by whitespace | ||
# (?= # Tag end delimiter (not consumed; is zero-length lookahead) | ||
# #{delimiter}\s* | d # Either a delimiter optionally followed by whitespace or | ||
# \z # string end | ||
# ) | ||
def single_quote_pattern | ||
/(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/ | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module ActsAsTaggableOn | ||
## | ||
# Returns a new TagList using the given tag string. | ||
# | ||
# Example: | ||
# tag_list = ActsAsTaggableOn::GenericParser.new.parse("One , Two, Three") | ||
# tag_list # ["One", "Two", "Three"] | ||
class GenericParser | ||
def initialize(tag_list) | ||
@tag_list = tag_list | ||
end | ||
|
||
def parse | ||
TagList.new.tap do |tag_list| | ||
tag_list.add @tag_list.split(',').map(&:strip).reject(&:empty?) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- encoding : utf-8 -*- | ||
require 'spec_helper' | ||
|
||
describe ActsAsTaggableOn::DefaultParser do | ||
it '#parse should return empty array if empty array is passed' do | ||
parser = ActsAsTaggableOn::DefaultParser.new([]) | ||
expect(parser.parse).to be_empty | ||
end | ||
|
||
describe 'Multiple Delimiter' do | ||
before do | ||
@old_delimiter = ActsAsTaggableOn.delimiter | ||
end | ||
|
||
after do | ||
ActsAsTaggableOn.delimiter = @old_delimiter | ||
end | ||
|
||
it 'should separate tags by delimiters' do | ||
ActsAsTaggableOn.delimiter = [',', ' ', '\|'] | ||
parser = ActsAsTaggableOn::DefaultParser.new('cool, data|I have') | ||
expect(parser.parse.to_s).to eq('cool, data, I, have') | ||
end | ||
|
||
it 'should escape quote' do | ||
ActsAsTaggableOn.delimiter = [',', ' ', '\|'] | ||
parser = ActsAsTaggableOn::DefaultParser.new("'I have'|cool, data") | ||
expect(parser.parse.to_s).to eq('"I have", cool, data') | ||
|
||
parser = ActsAsTaggableOn::DefaultParser.new('"I, have"|cool, data') | ||
expect(parser.parse.to_s).to eq('"I, have", cool, data') | ||
end | ||
|
||
it 'should work for utf8 delimiter and long delimiter' do | ||
ActsAsTaggableOn.delimiter = [',', '的', '可能是'] | ||
parser = ActsAsTaggableOn::DefaultParser.new('我的东西可能是不见了,还好有备份') | ||
expect(parser.parse.to_s).to eq('我, 东西, 不见了, 还好有备份') | ||
end | ||
|
||
it 'should work for multiple quoted tags' do | ||
ActsAsTaggableOn.delimiter = [','] | ||
parser = ActsAsTaggableOn::DefaultParser.new('"Ruby Monsters","eat Katzenzungen"') | ||
expect(parser.parse.to_s).to eq('Ruby Monsters, eat Katzenzungen') | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.