Class: MakeTaggable::DefaultParser
- Inherits:
-
GenericParser
- Object
- GenericParser
- MakeTaggable::DefaultParser
- Defined in:
- lib/make_taggable/default_parser.rb
Overview
Returns a new TagList using the given tag string.
Example:
tag_list = MakeTaggable::DefaultParser.parse("One , Two, Three")
tag_list # ["One", "Two", "Three"]
Instance Method Summary collapse
-
#delimiter ⇒ Object
private.
-
#double_quote_pattern ⇒ Object
( # 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) ##delimiters* | # Either a delimiter optionally followed by whitespace or z # string end ).
- #parse ⇒ Object
-
#single_quote_pattern ⇒ Object
( # 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) ##delimiters* | d # Either a delimiter optionally followed by whitespace or z # string end ).
Methods inherited from GenericParser
Constructor Details
This class inherits a constructor from MakeTaggable::GenericParser
Instance Method Details
#delimiter ⇒ Object
private
37 38 39 40 41 42 43 |
# File 'lib/make_taggable/default_parser.rb', line 37 def delimiter # Parse the quoted tags d = MakeTaggable.delimiter # Separate multiple delimiters by bitwise operator d = d.join("|") if d.is_a?(Array) d end |
#double_quote_pattern ⇒ Object
( # 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) ##delimiters* | # Either a delimiter optionally followed by whitespace or z # string end )
56 57 58 |
# File 'lib/make_taggable/default_parser.rb', line 56 def double_quote_pattern /(\A|#{delimiter})\s*"(.*?)"\s*(?=#{delimiter}\s*|\z)/ end |
#parse ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/make_taggable/default_parser.rb', line 9 def parse string = @tag_list string = string.join(MakeTaggable.glue) if string.respond_to?(:join) TagList.new.tap do |tag_list| string = string.to_s.dup string.gsub!(double_quote_pattern) do # Append the matched tag to the tag list tag_list << Regexp.last_match[2] # Return the matched delimiter ($3) to replace the matched items "" end string.gsub!(single_quote_pattern) do # Append the matched tag ($2) to the tag list tag_list << Regexp.last_match[2] # Return an empty string to replace the matched items "" end # split the string by the delimiter # and add to the tag_list tag_list.add(string.split(Regexp.new(delimiter))) end end |
#single_quote_pattern ⇒ Object
( # 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) ##delimiters* | d # Either a delimiter optionally followed by whitespace or z # string end )
71 72 73 |
# File 'lib/make_taggable/default_parser.rb', line 71 def single_quote_pattern /(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/ end |