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-<%= category %> a, a.tag-type-<%= category %> {
color: <%= cssmap["color"] %>;
.tag-type-0 a, a.tag-type-0 {
color: var(--general-tag-color);
color: var(--general-tag-hover-color);
&:link, &:visited {
color: <%= cssmap["color"] %>;
}
&:hover {
color: <%= cssmap["hover"] %>;
}
&:link, &:visited {
color: var(--general-tag-color);
}
<% 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"
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")
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")
when "rank"

View File

@@ -1,80 +1,97 @@
# Utility methods for working with tag categories (general, character,
# 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
def canonical_mapping
@@canonical_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [k.capitalize, v["category"]] }]
end
module TagCategory
module_function
# Returns a hash mapping numerical category values to their string equivalent.
def reverse_mapping
@@reverse_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [v["category"], k] }]
end
# Returns a hash mapping for the short name usage in metatags
def short_name_mapping
@@short_name_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [v["short"], k] }]
end
# Returns a hash mapping for related tag buttons (javascripts/related_tag.js.erb)
def related_button_mapping
@@related_button_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k, v| [k, v["relatedbutton"]] }]
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
# Returns a hash mapping various tag categories to a numerical value.
def mapping
{
"ch" => 4,
"co" => 3,
"gen" => 0,
"char" => 4,
"copy" => 3,
"art" => 1,
"meta" => 5,
"general" => 0,
"character" => 4,
"copyright" => 3,
"artist" => 1,
}
end
module Lists
def categories
@@categories ||= Danbooru.config.full_tag_config_info.keys
end
def category_ids
@@category_ids ||= canonical_mapping.values
end
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
# The order of tags in dropdown lists.
def canonical_mapping
{
"Artist" => 1,
"Copyright" => 3,
"Character" => 4,
"General" => 0,
"Meta" => 5,
}
end
module Regexes
def short_name_regex
@@short_name_regex ||= short_name_list.join("|")
end
def category_ids_regex
@@category_ids_regex ||= "[#{category_ids.join}]"
end
# Returns a hash mapping numerical category values to their string equivalent.
def reverse_mapping
{
0 => "general",
4 => "character",
3 => "copyright",
1 => "artist",
5 => "meta",
}
end
extend Mappings
extend Lists
extend Regexes
def short_name_mapping
{
"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

View File

@@ -215,83 +215,6 @@ module Danbooru
# StorageManager::SFTP.new("www.example.com", base_dir: "/mnt/backup", ssh_options: {})
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
# having to modify the templates.
def custom_html_header_content