Class: MakeTaggable::DefaultParser
- Inherits:
-
GenericParser
- Object
- GenericParser
- MakeTaggable::DefaultParser
- Defined in:
- lib/make_taggable/default_parser.rb
Overview
The parser the library uses unless told otherwise.
It splits on the configured delimiter, or delimiters, and understands quoting: a tag wrapped in single or double quotes may contain the delimiter itself.
Instance Method Summary collapse
-
#delimiter ⇒ String
private
The configured delimiters as a regular expression source, ready to interpolate into a pattern.
-
#double_quote_pattern ⇒ Regexp
private
Matches a double-quoted tag, bounded by the start of the string or a delimiter.
-
#parse ⇒ MakeTaggable::TagList
Splits the input into tags, honouring quotes and every configured delimiter.
-
#single_quote_pattern ⇒ Regexp
private
Matches a single-quoted tag, bounded by the start of the string or a delimiter.
Methods inherited from GenericParser
Constructor Details
This class inherits a constructor from MakeTaggable::GenericParser
Instance Method Details
#delimiter ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The configured delimiters as a regular expression source, ready to interpolate into a pattern.
58 59 60 61 62 63 64 |
# File 'lib/make_taggable/default_parser.rb', line 58 def delimiter # Delimiters are literal strings, so any regular expression metacharacter # they contain has to be escaped before it reaches a pattern. Regexp.union # handles both the escaping and the alternation between multiple # delimiters. Regexp.union(Array(MakeTaggable.delimiter)).source end |
#double_quote_pattern ⇒ Regexp
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Matches a double-quoted tag, bounded by the start of the string or a delimiter.
73 74 75 |
# File 'lib/make_taggable/default_parser.rb', line 73 def double_quote_pattern /(\A|#{delimiter})\s*"(.*?)"\s*(?=#{delimiter}\s*|\z)/ end |
#parse ⇒ MakeTaggable::TagList
Splits the input into tags, honouring quotes and every configured delimiter.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/make_taggable/default_parser.rb', line 24 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 ⇒ Regexp
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Matches a single-quoted tag, bounded by the start of the string or a delimiter.
84 85 86 |
# File 'lib/make_taggable/default_parser.rb', line 84 def single_quote_pattern /(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/ end |