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 {
color: <%= cssmap["hover"] %>;
}
} }
<% end %>
&: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);
}
}

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