Merge pull request #2125 from vikhyat/css-variables

Allow importing Discourse styles in custom stylesheets to modify vars
This commit is contained in:
Régis Hanol 2014-04-01 15:24:09 +02:00
commit b537de3ca5
60 changed files with 133 additions and 178 deletions

View file

@ -1,17 +0,0 @@
// Manifest
//
//= require ./vendor/normalize
//= require ./common/foundation/base
//= require ./vendor/font_awesome/font-awesome
//= require ./vendor/chosen
//= require_tree ./common/components
//= require ./common/foundation/helpers
//= require_tree ./common
<%
# TODO this is very tricky, we want to add a dependency here on files that may not yet exist
# otherwise in dev we are often stuck nuking the tmp/cache directory
DiscoursePluginRegistry.stylesheets.each do |css|
require_asset(css)
end
%>

View file

@ -0,0 +1,10 @@
@import "vendor/normalize";
@import "common/foundation/base";
@import "vendor/font_awesome/font-awesome";
@import "vendor/chosen";
@import "common/foundation/helpers";
@import "common/foundation/mixins";
@import "common/foundation/variables";
@import "common/components/*";
@import "common/admin/*";
@import "common/input_tip";

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
// --------------------------------------------------
// Badges
// --------------------------------------------------

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
// --------------------------------------------------
// Buttons
// --------------------------------------------------

View file

@ -1,5 +1,3 @@
@import "../foundation/variables";
.topic-list-item td:first-child, .topic-post {
background-color: inherit;
border-left: 1px solid transparent;

View file

@ -1,7 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
@import "common/foundation/helpers";
// --------------------------------------------------
// Navigation menus
// --------------------------------------------------

View file

@ -1,5 +0,0 @@
// Manifest
//
//= require common
//= require_tree ./desktop

View file

@ -0,0 +1,2 @@
@import "common";
@import "desktop/*";

View file

@ -1,9 +1,6 @@
// Styles used before the user is logged into discourse. For example, activating their
// account or changing their email.
@import "common/foundation/variables";
@import "common/foundation/mixins";
#simple-container {
@include border-radius-all(10px);
background-color: $primary_background_color;

View file

@ -1,8 +1,5 @@
// styles for the category badge color picker
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
.category-color-editor {
input {
width: 70px;

View file

@ -1,6 +1,3 @@
@import "../common/foundation/variables";
.category-combobox {
.badge-category {
display: inline-block;

View file

@ -1,8 +1,5 @@
// styles that apply to the reply pane that slides up to compose replies
@import "common/foundation/variables";
@import "common/foundation/mixins";
// hack, this needs to be done cleaner
#private-message-users {
width: 400px;

View file

@ -2,11 +2,6 @@
// BEWARE: changing these styles implies they take effect anywhere they are seen
// throughout the Discourse application
@import "common/foundation/variables";
@import "common/foundation/mixins";
@import "common/foundation/helpers";
html {font-size: 14px/19px;}
body {

View file

@ -1,7 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
@import "common/foundation/helpers";
// --------------------------------------------------
// FAQs
// --------------------------------------------------

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
// --------------------------------------------------
// Discourse header
// --------------------------------------------------

View file

@ -1,8 +1,5 @@
// styles that apply to the popup that appears when you show the edit history of a post
@import "common/foundation/variables";
@import "common/foundation/mixins";
.modal.history-modal {
.modal-inner-container {
min-width: 960px;

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
.lightbox {
position: relative;
display: inline-block;

View file

@ -1,8 +1,5 @@
// style that apply to the login popup
@import "common/foundation/variables";
@import "common/foundation/mixins";
#login-buttons {
button {
margin: 0 5px 5px 0;

View file

@ -1,8 +1,5 @@
// base styles for every modal popup used in Discourse
@import "common/foundation/variables";
@import "common/foundation/mixins";
.modal-open {
.dropdown-menu {
z-index: 2050;

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
a.loading-onebox {
background: {
image: image-url("spinner_96.gif");

View file

@ -1,8 +1,5 @@
// styles that apply to the PageDown editor
// http://code.google.com/p/pagedown/
@import "common/foundation/variables";
@import "common/foundation/mixins";
.wmd-panel {
margin-left: 25%;

View file

@ -1,8 +1,5 @@
// styles that apply to the "share" popup when sharing a link to a post or topic
@import "common/foundation/variables";
@import "common/foundation/mixins";
#poster-expansion {
position: absolute;
width: 460px;

View file

@ -1,8 +1,5 @@
// styles that apply to the "share" popup when sharing a link to a post or topic
@import "common/foundation/variables";
@import "common/foundation/mixins";
#share-link {
position: absolute;
left: 20px;

View file

@ -1,8 +1,5 @@
// Styles for the topic admin menu
@import "common/foundation/variables";
@import "common/foundation/mixins";
#show-topic-admin {
position: fixed;
top: 70px;

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
// --------------------------------------------------
// Topic lists
// --------------------------------------------------

View file

@ -1,7 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
@import "common/foundation/helpers";
.gap {
background-color: lighten($secondary_background_color, 76%);
padding: 5px 0;

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
.post-info a {
color: lighten($primary_text_color, 50%);
padding-right: 5px;

View file

@ -1,6 +1,3 @@
@import "common/foundation/variables";
@import "common/foundation/mixins";
.add-upload .fa-plus {
font-size: 10px;
position: relative;

View file

@ -1,6 +1,4 @@
// styles that apply to the user page
@import "common/foundation/variables";
@import "common/foundation/mixins";
.groups {
.group-link {

View file

@ -1,4 +0,0 @@
// Manifest
//
//= require common
//= require_tree ./mobile

View file

@ -0,0 +1,2 @@
@import "common";
@import "mobile/*";

View file

@ -1,9 +1,6 @@
// Styles used before the user is logged into discourse. For example, activating their
// account or changing their email.
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
#simple-container {
@include border-radius-all(10px);
background-color: $primary_background_color;

View file

@ -1,8 +1,5 @@
// styles for the category badge color picker
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
.category-color-editor {
input {
width: 70px;

View file

@ -1,8 +1,5 @@
// styles that apply to the reply pane that slides up to compose replies
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
// hack, this needs to be done cleaner
.private-message input.span8 {
width: 47%;

View file

@ -2,10 +2,6 @@
// BEWARE: changing these styles implies they take effect anywhere they are seen
// throughout the Discourse application
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
@import "../common/foundation/helpers";
body {
background-color: $primary_background_color;
}

View file

@ -1,7 +1,3 @@
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
@import "../common/foundation/helpers";
// --------------------------------------------------
// FAQs
// --------------------------------------------------

View file

@ -1,6 +1,3 @@
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
// --------------------------------------------------
// Discourse header
// --------------------------------------------------

View file

@ -1,8 +1,5 @@
// styles that apply to the popup that appears when you show the edit history of a post
@import "common/foundation/variables";
@import "common/foundation/mixins";
.modal.history-modal {
.modal-inner-container {
min-width: 960px;

View file

@ -1,6 +1,3 @@
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
.lightbox {
position: relative;
display: inline-block;

View file

@ -1,8 +1,5 @@
// style that apply to the login popup
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
.btn-social {
width: 250px;
font-size: 16px;

View file

@ -1,8 +1,5 @@
// base styles for every modal popup used in Discourse
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
.modal-open {
.dropdown-menu {
z-index: 2050;

View file

@ -1,6 +1,3 @@
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
a.loading-onebox {
background: {
image: image-url("spinner_96.gif");

View file

@ -1,8 +1,6 @@
// styles that apply to the PageDown editor
// http://code.google.com/p/pagedown/
@import "../common/foundation/mixins";
.wmd-panel {
margin-left: 25%;
margin-right: 25%;

View file

@ -1,8 +1,5 @@
// styles that apply to the "share" popup when sharing a link to a post or topic
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
#share-link {
position: absolute;
left: 20px;

View file

@ -1,8 +1,5 @@
// Styles for the topic admin menu
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
#show-topic-admin {
position: fixed;
top: 70px;

View file

@ -1,6 +1,3 @@
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
// --------------------------------------------------
// Topic lists
// --------------------------------------------------

View file

@ -1,5 +1,3 @@
@import "common/foundation/variables";
.gap {
background-color: lighten($secondary_background_color, 76%);
padding: 5px 15px;

View file

@ -1,5 +1,3 @@
@import "common/foundation/variables";
.topic-meta-data {
width: 100%;
h3 a {margin-left: 10px;}

View file

@ -1,6 +1,3 @@
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
.add-upload .fa-plus {
font-size: 10px;
position: relative;

View file

@ -1,6 +1,4 @@
// styles that apply to the user page
@import "../common/foundation/variables";
@import "../common/foundation/mixins";
.user-preferences {
.control-group {

View file

@ -0,0 +1,7 @@
<%
# TODO this is very tricky, we want to add a dependency here on files that may not yet exist
# otherwise in dev we are often stuck nuking the tmp/cache directory
DiscoursePluginRegistry.stylesheets.each do |css|
require_asset(css)
end
%>

View file

@ -60,7 +60,7 @@
width: 12px;
height: 13px;
font-size: 1px;
background: url(<%=asset_path "chosen-sprite.png"%>) right top no-repeat;
background: asset-url("chosen-sprite.png") right top no-repeat;
}
.chzn-container-single .chzn-single abbr:hover {
background-position: right -11px;
@ -74,7 +74,7 @@
width: 18px;
}
.chzn-container-single .chzn-single div b {
background: url(<%=asset_path "chosen-sprite.png"%>) no-repeat 0 0;
background: asset-url("chosen-sprite.png") no-repeat 0 0;
display: block;
width: 100%;
height: 100%;
@ -184,7 +184,7 @@
width: 12px;
height: 13px;
font-size: 1px;
background: url(<%=asset_path "chosen-sprite.png"%>) right top no-repeat;
background: asset-url("chosen-sprite.png") right top no-repeat;
}
.chzn-container-multi .chzn-choices .search-choice .search-choice-close:hover {
background-position: right -11px;
@ -264,10 +264,10 @@
bottom: 0;
}
.chzn-container .chzn-results-scroll-down span {
background: url(<%=asset_path "chosen-sprite.png"%>) no-repeat -4px -3px;
background: asset-url("chosen-sprite.png") no-repeat -4px -3px;
}
.chzn-container .chzn-results-scroll-up span {
background: url(<%=asset_path "chosen-sprite.png"%>) no-repeat -22px -3px;
background: asset-url("chosen-sprite.png") no-repeat -22px -3px;
}
/* @end */

View file

@ -1,3 +1,5 @@
require_dependency 'discourse_sass_importer'
class SiteCustomization < ActiveRecord::Base
ENABLED_KEY = '7e202ef2-56d7-47d5-98d8-a9c8d15e57dd'
# placing this in uploads to ease deployment rules
@ -11,11 +13,21 @@ class SiteCustomization < ActiveRecord::Base
true
end
def compile_stylesheet(scss)
::Sass::Engine.new(scss, {
syntax: :scss,
cache: false,
read_cache: false,
style: :compressed,
filesystem_importer: DiscourseSassImporter
}).render
end
before_save do
['stylesheet', 'mobile_stylesheet'].each do |stylesheet_attr|
if self.send("#{stylesheet_attr}_changed?")
begin
self.send("#{stylesheet_attr}_baked=", Sass.compile(self.send(stylesheet_attr)))
self.send("#{stylesheet_attr}_baked=", compile_stylesheet(self.send(stylesheet_attr)))
rescue Sass::SyntaxError => e
error = e.sass_backtrace_str("custom stylesheet")
error.gsub!("\n", '\A ')

View file

@ -6,9 +6,11 @@
<% end %>
<%- end %>
<%= stylesheet_link_tag "plugins" %>
<%- if staff? %>
<%= stylesheet_link_tag "admin"%>
<%-end%>
<%- end %>
<%- unless customization_disabled? %>
<%= SiteCustomization.custom_stylesheet(session[:preview_style], mobile_view? ? :mobile : :desktop) %>

View file

@ -0,0 +1,71 @@
# This custom importer is used for site customizations. This is similar to the
# Sprockets::SassImporter implementation provided in sass-rails since that is used
# during asset precompilation.
class DiscourseSassImporter < Sass::Importers::Filesystem
GLOB = /\*|\[.+\]/
def initialize(root)
@root = Rails.root.join('app', 'assets', 'stylesheets').to_s
@same_name_warnings = Set.new
end
def extensions
{
'css' => :scss,
'css.scss' => :scss,
'css.sass' => :sass,
'css.erb' => :scss,
'scss.erb' => :scss,
'sass.erb' => :sass,
'css.scss.erb' => :scss,
'css.sass.erb' => :sass
}.merge!(super)
end
def find_relative(name, base, options)
if name =~ GLOB
glob_imports(name, Pathname.new(base), options)
else
engine_from_path(name, File.dirname(base), options)
end
end
def find(name, options)
if name =~ GLOB
nil # globs must be relative
else
engine_from_path(name, root, options)
end
end
def each_globbed_file(glob, base_pathname, options)
Dir["#{base_pathname}/#{glob}"].sort.each do |filename|
next if filename == options[:filename]
yield filename # assume all matching files are requirable
end
end
def glob_imports(glob, base_pathname, options)
contents = ""
each_globbed_file(glob, base_pathname.dirname, options) do |filename|
unless File.directory?(filename)
contents << "@import #{Pathname.new(filename).relative_path_from(base_pathname.dirname).to_s.inspect};\n"
end
end
return nil if contents.empty?
Sass::Engine.new(contents, options.merge(
filename: base_pathname.to_s,
importer: self,
syntax: :scss
))
end
private
def engine_from_path(name, dir, options)
full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options))
return unless full_filename && File.readable?(full_filename)
Sass::Engine.for_file(full_filename, options)
end
end

View file

@ -35,10 +35,10 @@ task 'assets:precompile:before' do
def evaluate(context, locals, &block)
::Sprockets.cache_compiled("sass", data) do
# HACK, SASS compiler will degrade to aweful perf with huge files
# Bypass if larger than 200kb, ensure assets are minified prior
# Bypass if larger than 500kb, ensure assets are minified prior
if context.pathname &&
context.pathname.to_s =~ /.css$/ &&
data.length > 200.kilobytes
data.length > 500.kilobytes
puts "Skipped minifying #{context.pathname} cause it is larger than 200KB, minify in source control or avoid large CSS files"
data
else

View file

@ -155,12 +155,20 @@ describe SiteCustomization do
it 'should compile scss' do
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '$black: #000; #a { color: $black; }', header: '')
c.stylesheet_baked.should == "#a {\n color: black; }\n"
c.stylesheet_baked.should == "#a{color:#000}\n"
end
it 'should compile mobile scss' do
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '', header: '', mobile_stylesheet: '$black: #000; #a { color: $black; }', mobile_header: '')
c.mobile_stylesheet_baked.should == "#a {\n color: black; }\n"
c.mobile_stylesheet_baked.should == "#a{color:#000}\n"
end
it 'should allow including discourse styles' do
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '@import "desktop";', mobile_stylesheet: '@import "mobile";')
c.stylesheet_baked.should_not =~ /Syntax error/
c.stylesheet_baked.length.should > 1000
c.mobile_stylesheet_baked.should_not =~ /Syntax error/
c.mobile_stylesheet_baked.length.should > 1000
end
it 'should provide an awesome error on failure' do