Class: Stylesheet::Importer
- Inherits:
-
Object
- Object
- Stylesheet::Importer
- Includes:
- GlobalPath
- Defined in:
- lib/stylesheet/importer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #category_backgrounds ⇒ Object
- #category_css(category) ⇒ Object
- #color_variables ⇒ Object
- #font ⇒ Object
- #font_css(font) ⇒ Object
- #import_color_definitions ⇒ Object
- #import_wcag_overrides ⇒ Object
-
#initialize(options) ⇒ Importer
constructor
A new instance of Importer.
- #prepended_scss ⇒ Object
- #public_image_path ⇒ Object
- #theme ⇒ Object
- #theme_import(target) ⇒ Object
- #wizard_fonts ⇒ Object
Methods included from GlobalPath
#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path
Constructor Details
#initialize(options) ⇒ Importer
Returns a new instance of Importer.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/stylesheet/importer.rb', line 178 def initialize() @theme = [:theme] @theme_id = [:theme_id] @color_scheme_id = [:color_scheme_id] if @theme && !@theme_id # make up an id so other stuff does not bail out @theme_id = @theme.id || -1 end end |
Class Method Details
.plugin_assets ⇒ Object
11 12 13 |
# File 'lib/stylesheet/importer.rb', line 11 def self.plugin_assets @plugin_assets ||= {} end |
.register_imports! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/stylesheet/importer.rb', line 15 def self.register_imports! Discourse.plugins.each do |plugin| plugin_directory_name = plugin.directory_name ["", "mobile", "desktop"].each do |type| asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_name stylesheets = ( if type.present? DiscoursePluginRegistry.send("#{type}_stylesheets") else DiscoursePluginRegistry.stylesheets end ) plugin_assets[asset_name] = stylesheets[ plugin_directory_name ] if plugin_directory_name.present? end end end |
Instance Method Details
#category_backgrounds ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/stylesheet/importer.rb', line 98 def category_backgrounds contents = +"" Category .where("uploaded_background_id IS NOT NULL") .each { |c| contents << category_css(c) if c.uploaded_background&.url.present? } contents end |
#category_css(category) ⇒ Object
218 219 220 221 |
# File 'lib/stylesheet/importer.rb', line 218 def category_css(category) full_slug = category.full_slug.split("-")[0..-2].join("-") "body.category-#{full_slug} { background-image: url(#{upload_cdn_path(category.uploaded_background.url)}) }\n" end |
#color_variables ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/stylesheet/importer.rb', line 144 def color_variables contents = +"" if @color_scheme_id colors = begin ColorScheme.find(@color_scheme_id).resolved_colors rescue StandardError ColorScheme.base_colors end elsif (@theme_id && !theme.component) colors = theme&.color_scheme&.resolved_colors || ColorScheme.base_colors else # this is a slightly ugly backwards compatibility fix, # we shouldn't be using the default theme color scheme for components # (most components use CSS custom properties which work fine without this) colors = Theme.find_by_id(SiteSetting.default_theme_id)&.color_scheme&.resolved_colors || ColorScheme.base_colors end colors.each { |n, hex| contents << "$#{n}: ##{hex} !default; " } contents end |
#font ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/stylesheet/importer.rb', line 39 def font body_font = DiscourseFonts.fonts.find { |f| f[:key] == SiteSetting.base_font } heading_font = DiscourseFonts.fonts.find { |f| f[:key] == SiteSetting.heading_font } contents = +"" contents << <<~CSS if body_font.present? #{font_css(body_font)} :root { --font-family: #{body_font[:stack]}; } CSS contents << <<~CSS if heading_font.present? #{font_css(heading_font)} :root { --heading-font-family: #{heading_font[:stack]}; } CSS contents end |
#font_css(font) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/stylesheet/importer.rb', line 223 def font_css(font) contents = +"" if font[:variants].present? fonts_dir = UrlHelper.absolute("#{Discourse.base_path}/fonts") font[:variants].each do |variant| src = ( if variant[:src] variant[:src] else "url(\"#{fonts_dir}/#{variant[:filename]}?v=#{DiscourseFonts::VERSION}\") format(\"#{variant[:format]}\")" end ) contents << <<~CSS @font-face { font-family: #{font[:name]}; src: #{src}; font-weight: #{variant[:weight]}; } CSS end end contents end |
#import_color_definitions ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/stylesheet/importer.rb', line 107 def import_color_definitions contents = +"" DiscoursePluginRegistry.color_definition_stylesheets.each do |name, path| contents << "\n\n// Color definitions from #{name}\n\n" contents << File.read(path.to_s) contents << "\n\n" end theme_id = @theme_id || SiteSetting.default_theme_id resolved_ids = Theme.transform_ids(theme_id) if resolved_ids theme = Theme.find_by_id(theme_id) contents << theme&.scss_variables.to_s Theme .list_baked_fields(resolved_ids, :common, :color_definitions) .each do |field| contents << "\n\n// Color definitions from #{field.theme.name}\n\n" if field.theme_id == theme.id contents << field.value else contents << field.compiled_css(prepended_scss) end contents << "\n\n" end end contents end |
#import_wcag_overrides ⇒ Object
137 138 139 140 141 142 |
# File 'lib/stylesheet/importer.rb', line 137 def import_wcag_overrides if @color_scheme_id && ColorScheme.find_by_id(@color_scheme_id)&.is_wcag? return "@import \"wcag\";" end "" end |
#prepended_scss ⇒ Object
174 175 176 |
# File 'lib/stylesheet/importer.rb', line 174 def prepended_scss "#{color_variables} #{public_image_path} @import \"common/foundation/variables\"; @import \"common/foundation/mixins\"; " end |
#public_image_path ⇒ Object
169 170 171 172 |
# File 'lib/stylesheet/importer.rb', line 169 def public_image_path image_path = UrlHelper.absolute("#{Discourse.base_path}/images") "$public_image_path: \"#{image_path}\"; " end |
#theme ⇒ Object
213 214 215 216 |
# File 'lib/stylesheet/importer.rb', line 213 def theme @theme = (@theme_id && Theme.find(@theme_id)) || :nil unless @theme @theme == :nil ? nil : @theme end |
#theme_import(target) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/stylesheet/importer.rb', line 189 def theme_import(target) return "" if !@theme_id attr = target == :embedded_theme ? :embedded_scss : :scss target = target.to_s.gsub("_theme", "").to_sym contents = +"" fields = theme.list_baked_fields(target, attr) fields.map do |field| value = field.value if value.present? contents << <<~SCSS // Theme: #{field.theme.name} // Target: #{field.target_name} #{field.name} // Last Edited: #{field.updated_at} SCSS contents << value end end contents end |
#wizard_fonts ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/stylesheet/importer.rb', line 63 def wizard_fonts contents = +"" DiscourseFonts.fonts.each do |font| if font[:key] == "system" # Overwrite font definition because the preview canvases in the wizard require explicit @font-face definitions. # uses same technique as https://github.com/jonathantneal/system-font-css font[:variants] = [ { src: 'local(".SFNS-Regular"), local(".SFNSText-Regular"), local(".HelveticaNeueDeskInterface-Regular"), local(".LucidaGrandeUI"), local("Segoe UI"), local("Ubuntu"), local("Roboto-Regular"), local("DroidSans"), local("Tahoma")', weight: 400, }, { src: 'local(".SFNS-Bold"), local(".SFNSText-Bold"), local(".HelveticaNeueDeskInterface-Bold"), local(".LucidaGrandeUI"), local("Segoe UI Bold"), local("Ubuntu Bold"), local("Roboto-Bold"), local("DroidSans-Bold"), local("Tahoma Bold")', weight: 700, }, ] end contents << font_css(font) contents << <<~CSS .body-font-#{font[:key].tr("_", "-")} { font-family: #{font[:stack]}; } .heading-font-#{font[:key].tr("_", "-")} h2 { font-family: #{font[:stack]}; } CSS end contents end |