Expand the tag abbreviation system introduced in b0be8ae45 so that it
works in searches and when tagging posts, not just in autocomplete.
For example, you can tag a post with /evth and it will add the tag
eyebrows_visible_through_hair. You can search for /evth and it will
search for the tag eyebrows_visible_through_hair.
Some more examples:
* /ops is short for one-piece_swimsuit
* /hooe is short for hair_over_one_eye
* /saol is short for standing_on_one_leg
* /tlozbotw is short for the_legend_of_zelda:_breath_of_the_wild
If two tags have the same abbreviation, then the larger tag takes
precedence. For example, /be is short for blue_eyes, not brown_eyes,
because blue_eyes is the bigger tag.
If there is an existing shortcut alias that conflicts with the
abbreviation, then the alias take precedence. For example, /sh is short
for suzumiya_haruhi, not short_hair, because there's an old alias for
/sh -> suzumiya_haruhi.
57 lines
1.2 KiB
Ruby
57 lines
1.2 KiB
Ruby
module Danbooru
|
|
module Extensions
|
|
module String
|
|
def to_escaped_for_sql_like
|
|
string = self.gsub(/%|_|\*|\\\*|\\\\|\\/) do |str|
|
|
case str
|
|
when '%' then '\%'
|
|
when '_' then '\_'
|
|
when '*' then '%'
|
|
when '\*' then '*'
|
|
when '\\\\' then '\\\\'
|
|
when '\\' then '\\\\'
|
|
end
|
|
end
|
|
|
|
string
|
|
end
|
|
|
|
# escape \ and * characters so that they're treated literally in LIKE searches.
|
|
def escape_wildcards
|
|
gsub(/\\/, '\\\\').gsub(/\*/, '\*')
|
|
end
|
|
|
|
def to_escaped_for_tsquery_split
|
|
scan(/\S+/).map {|x| x.to_escaped_for_tsquery}.join(" & ")
|
|
end
|
|
|
|
def to_escaped_for_tsquery
|
|
"'#{gsub(/\0/, '').gsub(/'/, '\0\0').gsub(/\\/, '\0\0\0\0')}'"
|
|
end
|
|
|
|
def truthy?
|
|
self.match?(/\A(true|t|yes|y|on|1)\z/i)
|
|
end
|
|
|
|
def falsy?
|
|
self.match?(/\A(false|f|no|n|off|0)\z/i)
|
|
end
|
|
|
|
def ilike?(pattern)
|
|
pattern = Regexp.escape(pattern).gsub(/\\\*/, ".*")
|
|
match?(/\A#{pattern}\z/i)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
class String
|
|
include Danbooru::Extensions::String
|
|
end
|
|
|
|
class FalseClass
|
|
def to_i
|
|
0
|
|
end
|
|
end
|