Skip to content
Permalink
372a0da981
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
40 lines (33 sloc) 956 Bytes
# frozen_string_literal: true
# Simplified version of https://github.com/marcelocf/searrrch/blob/f2825e26/lib/searrrch.rb
class SearchParser
OPERATOR_EXPRESSION = /(\-?\w+):[\  ]?([\w\p{Han}\p{Katakana}\p{Hiragana}\p{Hangul}ー\.\-,\/]+|(["'])(\\?.)*?\3)/
attr_accessor :freetext
attr_accessor :operators
def initialize(query)
query = query.to_s
@operators = {}
offset = 0
while (m = OPERATOR_EXPRESSION.match(query, offset))
key = m[1].downcase.to_sym
value = m[2]
offset = m.end(2)
@operators[key] ||= []
value.split(',').each{ |v| @operators[key] << v }
end
@freetext = query[offset, query.length].strip
end
def [](key)
values = @operators[key.to_sym] || []
values.map do |value|
if ["'", '"'].include?(value[0])
value[1, value.length - 2]
else
value
end
end
end
def []=(key, value)
@operators[key.to_sym] = value
end
end