tags: move tag category definitions out of the config file.

Move all the code for defining tag categories from the config file to
TagCategory. It didn't belong in the config because it's not possible to
add new tag categories purely in the config without editing other things
like the CSS.

Also change it so that tag colors are hardcoded in the CSS instead of
generated using ERB. Generating the CSS in ERB meant that the Docker
build had to recompile the CSS on every commit, even when it didn't
change, because it relied on Ruby code outside the CSS that we couldn't
guarantee didn't change.
This commit is contained in:
evazion
2021-09-30 10:03:33 -05:00
parent e72446463e
commit 587a9d0c8f
4 changed files with 152 additions and 159 deletions

View File

@@ -1,13 +1,64 @@
<% TagCategory.css_mapping.each do |category,cssmap| %> .tag-type-0 a, a.tag-type-0 {
.tag-type-<%= category %> a, a.tag-type-<%= category %> { color: var(--general-tag-color);
color: <%= cssmap["color"] %>; color: var(--general-tag-hover-color);
&:link, &:visited { &:link, &:visited {
color: <%= cssmap["color"] %>; color: var(--general-tag-color);
} }
&:hover { &:hover {
color: <%= cssmap["hover"] %>; color: var(--general-tag-hover-color);
}
}
.tag-type-1 a, a.tag-type-1 {
color: var(--artist-tag-color);
color: var(--artist-tag-hover-color);
&:link, &:visited {
color: var(--artist-tag-color);
}
&:hover {
color: var(--artist-tag-hover-color);
}
}
.tag-type-3 a, a.tag-type-3 {
color: var(--copyright-tag-color);
color: var(--copyright-tag-hover-color);
&:link, &:visited {
color: var(--copyright-tag-color);
}
&:hover {
color: var(--copyright-tag-hover-color);
}
}
.tag-type-4 a, a.tag-type-4 {
color: var(--character-tag-color);
color: var(--character-tag-hover-color);
&:link, &:visited {
color: var(--character-tag-color);
}
&:hover {
color: var(--character-tag-hover-color);
}
}
.tag-type-5 a, a.tag-type-5 {
color: var(--meta-tag-color);
color: var(--meta-tag-hover-color);
&:link, &:visited {
color: var(--meta-tag-color);
}
&:hover {
color: var(--meta-tag-hover-color);
} }
} }
<% end %>

View File

@@ -665,10 +665,12 @@ class PostQueryBuilder
when "duration_asc" when "duration_asc"
relation = relation.joins(:media_asset).order("media_assets.duration ASC NULLS LAST, posts.id ASC") relation = relation.joins(:media_asset).order("media_assets.duration ASC NULLS LAST, posts.id ASC")
when /(#{TagCategory.short_name_regex})tags(?:\Z|_desc)/ # artags_desc, copytags_desc, chartags_desc, gentags_desc, metatags_desc
when /(#{TagCategory.short_name_list.join("|")})tags(?:\Z|_desc)/
relation = relation.order("posts.tag_count_#{TagCategory.short_name_mapping[$1]} DESC") relation = relation.order("posts.tag_count_#{TagCategory.short_name_mapping[$1]} DESC")
when /(#{TagCategory.short_name_regex})tags_asc/ # artags_asc, copytags_asc, chartags_asc, gentags_asc, metatags_asc
when /(#{TagCategory.short_name_list.join("|")})tags_asc/
relation = relation.order("posts.tag_count_#{TagCategory.short_name_mapping[$1]} ASC") relation = relation.order("posts.tag_count_#{TagCategory.short_name_mapping[$1]} ASC")
when "rank" when "rank"

View File

@@ -1,80 +1,97 @@
# Utility methods for working with tag categories (general, character, # Utility methods for working with tag categories (general, character,
# copyright, artist, meta). # copyright, artist, meta).
class TagCategory
module Mappings module TagCategory
module_function
# Returns a hash mapping various tag categories to a numerical value. # Returns a hash mapping various tag categories to a numerical value.
def mapping def mapping
@@mapping ||= {
Hash[ "ch" => 4,
Danbooru.config.full_tag_config_info.map { |k, v| v["extra"].map { |y| [y, v["category"]] }}.reduce([], :+) "co" => 3,
] "gen" => 0,
.update(Hash[Danbooru.config.full_tag_config_info.map { |k, v| [v["short"], v["category"]] }]) "char" => 4,
.update(Hash[Danbooru.config.full_tag_config_info.map { |k, v| [k, v["category"]] }]) "copy" => 3,
"art" => 1,
"meta" => 5,
"general" => 0,
"character" => 4,
"copyright" => 3,
"artist" => 1,
}
end end
# Returns a hash mapping more suited for views # The order of tags in dropdown lists.
def canonical_mapping def canonical_mapping
@@canonical_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [k.capitalize, v["category"]] }] {
"Artist" => 1,
"Copyright" => 3,
"Character" => 4,
"General" => 0,
"Meta" => 5,
}
end end
# Returns a hash mapping numerical category values to their string equivalent. # Returns a hash mapping numerical category values to their string equivalent.
def reverse_mapping def reverse_mapping
@@reverse_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [v["category"], k] }] {
0 => "general",
4 => "character",
3 => "copyright",
1 => "artist",
5 => "meta",
}
end end
# Returns a hash mapping for the short name usage in metatags
def short_name_mapping def short_name_mapping
@@short_name_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [v["short"], k] }] {
"art" => "artist",
"copy" => "copyright",
"char" => "character",
"gen" => "general",
"meta" => "meta",
}
end end
# Returns a hash mapping for related tag buttons (javascripts/related_tag.js.erb) # Returns a hash mapping for related tag buttons (javascripts/related_tag.js.erb)
def related_button_mapping def related_button_mapping
@@related_button_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [k, v["relatedbutton"]] }] {
"general" => "General",
"character" => "Characters",
"copyright" => "Copyrights",
"artist" => "Artists",
"meta" => nil,
}
end end
# Returns a hash mapping for CSS (stylesheets/posts.scss.erb)
def css_mapping
@@css_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [v["category"], v["css"]] }]
end
end
module Lists
def categories def categories
@@categories ||= Danbooru.config.full_tag_config_info.keys %w[general character copyright artist meta]
end end
def category_ids def category_ids
@@category_ids ||= canonical_mapping.values canonical_mapping.values
end end
def short_name_list def short_name_list
@@short_name_list ||= short_name_mapping.keys %w[art copy char gen meta]
end end
# The order of tags on the post page tag list.
def split_header_list def split_header_list
Danbooru.config.split_tag_header_list %w[artist copyright character general meta]
end end
# The order of tags inside the tag edit box, and on the comments page.
def categorized_list def categorized_list
Danbooru.config.categorized_tag_list %w[artist copyright character meta general]
end end
# The order of tags in the related tag buttons.
def related_button_list def related_button_list
Danbooru.config.related_tag_button_list %w[general artist character copyright]
end
end
module Regexes
def short_name_regex
@@short_name_regex ||= short_name_list.join("|")
end end
def category_ids_regex def category_ids_regex
@@category_ids_regex ||= "[#{category_ids.join}]" "[#{category_ids.join}]"
end end
end end
extend Mappings
extend Lists
extend Regexes
end

View File

@@ -215,83 +215,6 @@ module Danbooru
# StorageManager::SFTP.new("www.example.com", base_dir: "/mnt/backup", ssh_options: {}) # StorageManager::SFTP.new("www.example.com", base_dir: "/mnt/backup", ssh_options: {})
end end
# TAG CONFIGURATION
# Full tag configuration info for all tags
def full_tag_config_info
@full_tag_category_mapping ||= {
"general" => {
"category" => 0,
"short" => "gen",
"extra" => [],
"relatedbutton" => "General",
"css" => {
"color" => "var(--general-tag-color)",
"hover" => "var(--general-tag-hover-color)"
}
},
"character" => {
"category" => 4,
"short" => "char",
"extra" => ["ch"],
"relatedbutton" => "Characters",
"css" => {
"color" => "var(--character-tag-color)",
"hover" => "var(--character-tag-hover-color)"
}
},
"copyright" => {
"category" => 3,
"short" => "copy",
"extra" => ["co"],
"relatedbutton" => "Copyrights",
"css" => {
"color" => "var(--copyright-tag-color)",
"hover" => "var(--copyright-tag-hover-color)"
}
},
"artist" => {
"category" => 1,
"short" => "art",
"extra" => [],
"relatedbutton" => "Artists",
"css" => {
"color" => "var(--artist-tag-color)",
"hover" => "var(--artist-tag-hover-color)"
}
},
"meta" => {
"category" => 5,
"short" => "meta",
"extra" => [],
"relatedbutton" => nil,
"css" => {
"color" => "var(--meta-tag-color)",
"hover" => "var(--meta-tag-hover-color)"
}
}
}
end
# TAG ORDERS
# Sets the order of the split tag header list (presenters/tag_set_presenter.rb)
def split_tag_header_list
@split_tag_header_list ||= ["artist", "copyright", "character", "general", "meta"]
end
# Sets the order of the categorized tag string (presenters/post_presenter.rb)
def categorized_tag_list
@categorized_tag_list ||= ["artist", "copyright", "character", "meta", "general"]
end
# Sets the order of the related tag buttons (javascripts/related_tag.js)
def related_tag_button_list
@related_tag_button_list ||= ["general", "artist", "character", "copyright"]
end
# END TAG
# Any custom code you want to insert into the default layout without # Any custom code you want to insert into the default layout without
# having to modify the templates. # having to modify the templates.
def custom_html_header_content def custom_html_header_content