Fix tag ordering in humanized_essential_tag_string.

* Pick the largest character or copyright tags by post count. Previously
  we picked the tags with the longest names, which was nonsensical.

* Remove tag cateogory logic from config file. We can't avoid hardcoding
  some knowledge about tag categories here, so there's no point in trying.

This affects tab titles on post show pages as well as filenames in
downloaded images.
This commit is contained in:
evazion
2019-10-26 02:26:43 -05:00
parent ac775e2bdd
commit 3a544ba5e0
4 changed files with 51 additions and 91 deletions

View File

@@ -24,11 +24,6 @@ class TagCategory
@@short_name_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [v["short"],k]}]
end
# Returns a hash mapping for humanized_essential_tag_string (models/post.rb)
def humanized_mapping
@@humanized_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["humanized"]]}]
end
# Returns a hash mapping for split_tag_list_html (presenters/tag_set_presenter.rb)
def header_mapping
@@header_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["header"]]}]
@@ -58,10 +53,6 @@ class TagCategory
@@short_name_list ||= short_name_mapping.keys
end
def humanized_list
Danbooru.config.humanized_tag_category_list
end
def split_header_list
Danbooru.config.split_tag_header_list
end

View File

@@ -218,46 +218,52 @@ class Tag < ApplicationRecord
end
end
module NameMethods
def normalize_name(name)
name.to_s.mb_chars.downcase.strip.tr(" ", "_").to_s
concerning :NameMethods do
def unqualified_name
name.gsub(/_\(.*\)\z/, "").tr("_", " ")
end
def create_for_list(names)
names.map {|x| find_or_create_by_name(x).name}
end
def find_or_create_by_name(name, creator: CurrentUser.user)
name = normalize_name(name)
category = nil
if name =~ /\A(#{categories.regexp}):(.+)\Z/
category = $1
name = $2
class_methods do
def normalize_name(name)
name.to_s.mb_chars.downcase.strip.tr(" ", "_").to_s
end
tag = find_by_name(name)
def create_for_list(names)
names.map {|x| find_or_create_by_name(x).name}
end
if tag
if category
category_id = categories.value_for(category)
def find_or_create_by_name(name, creator: CurrentUser.user)
name = normalize_name(name)
category = nil
# in case a category change hasn't propagated to this server yet,
# force an update the local cache. This may get overwritten in the
# next few lines if the category is changed.
tag.update_category_cache
if tag.editable_by?(creator)
tag.update(category: category_id)
end
if name =~ /\A(#{categories.regexp}):(.+)\Z/
category = $1
name = $2
end
tag
else
Tag.new.tap do |t|
t.name = name
t.category = categories.value_for(category)
t.save
tag = find_by_name(name)
if tag
if category
category_id = categories.value_for(category)
# in case a category change hasn't propagated to this server yet,
# force an update the local cache. This may get overwritten in the
# next few lines if the category is changed.
tag.update_category_cache
if tag.editable_by?(creator)
tag.update(category: category_id)
end
end
tag
else
Tag.new.tap do |t|
t.name = name
t.category = categories.value_for(category)
t.save
end
end
end
end
@@ -921,7 +927,6 @@ class Tag < ApplicationRecord
include CountMethods
include CategoryMethods
extend StatisticsMethods
extend NameMethods
extend ParseMethods
extend SearchMethods
end

View File

@@ -61,33 +61,22 @@ class TagSetPresenter < Presenter
end.reject(&:blank?).join(" \n")
end
def humanized_essential_tag_string(category_list: TagCategory.humanized_list, default: "")
strings = category_list.map do |category|
mapping = TagCategory.humanized_mapping[category]
max_tags = mapping["slice"]
regexmap = mapping["regexmap"]
formatstr = mapping["formatstr"]
excluded_tags = mapping["exclusion"]
def humanized_essential_tag_string(default: "")
chartags = tags_for_category("character")
characters = chartags.max_by(5, &:post_count).map(&:unqualified_name)
characters += ["#{chartags.size - 5} more"] if chartags.size > 5
characters = characters.to_sentence
type_tags = tags_for_category(category).map(&:name) - excluded_tags
next if type_tags.empty?
copytags = tags_for_category("copyright")
copyrights = copytags.max_by(1, &:post_count).map(&:unqualified_name)
copyrights += ["#{copytags.size - 1} more"] if copytags.size > 1
copyrights = copyrights.to_sentence
copyrights = "(#{copyrights})" if characters.present?
if max_tags > 0 && type_tags.length > max_tags
type_tags = type_tags.sort_by {|x| -x.size}.take(max_tags) + ["etc"]
end
artists = tags_for_category("artist").map(&:name).grep_v("banned_artist").to_sentence
artists = "drawn by #{artists}" if artists.present?
if regexmap != //
type_tags = type_tags.map { |tag| tag.match(regexmap)[1] }
end
if category == "copyright" && tags_for_category("character").blank?
type_tags.to_sentence
else
formatstr % type_tags.to_sentence
end
end
strings = strings.compact.join(" ").tr("_", " ")
strings = "#{characters} #{copyrights} #{artists}"
strings.blank? ? default : strings
end

View File

@@ -247,7 +247,6 @@ module Danbooru
"short" => "gen",
"extra" => [],
"header" => %{<h1 class="general-tag-list">Tags</h1>},
"humanized" => nil,
"relatedbutton" => "General",
"css" => {
"color" => "var(--general-tag-color)",
@@ -259,12 +258,6 @@ module Danbooru
"short" => "char",
"extra" => ["ch"],
"header" => %{<h2 class="character-tag-list">Characters</h2>},
"humanized" => {
"slice" => 5,
"exclusion" => [],
"regexmap" => /^(.+?)(?:_\(.+\))?$/,
"formatstr" => "%s"
},
"relatedbutton" => "Characters",
"css" => {
"color" => "var(--character-tag-color)",
@@ -276,12 +269,6 @@ module Danbooru
"short" => "copy",
"extra" => ["co"],
"header" => %{<h2 class="copyright-tag-list">Copyrights</h2>},
"humanized" => {
"slice" => 1,
"exclusion" => [],
"regexmap" => //,
"formatstr" => "(%s)"
},
"relatedbutton" => "Copyrights",
"css" => {
"color" => "var(--copyright-tag-color)",
@@ -293,12 +280,6 @@ module Danbooru
"short" => "art",
"extra" => [],
"header" => %{<h2 class="artist-tag-list">Artists</h2>},
"humanized" => {
"slice" => 0,
"exclusion" => %w(banned_artist),
"regexmap" => //,
"formatstr" => "drawn by %s"
},
"relatedbutton" => "Artists",
"css" => {
"color" => "var(--artist-tag-color)",
@@ -310,7 +291,6 @@ module Danbooru
"short" => "meta",
"extra" => [],
"header" => %{<h2 class="meta-tag-list">Meta</h2>},
"humanized" => nil,
"relatedbutton" => nil,
"css" => {
"color" => "var(--meta-tag-color)",
@@ -322,11 +302,6 @@ module Danbooru
#TAG ORDERS
#Sets the order of the humanized essential tag string (models/post.rb)
def humanized_tag_category_list
@humanized_tag_category_list ||= ["character","copyright","artist"]
end
#Sets the order of the split tag header list (presenters/tag_set_presenter.rb)
def split_tag_header_list
@split_tag_header_list ||= ["copyright","character","artist","general","meta"]