Remove user_color_scheme_class

Instead of rendering this value server-side, we use Javascript and Gon
to apply the user's color scheme (or the default) to any syntax
highlighted code blocks.

This will make it easier to cache these blocks in the future because
they're no longer state-dependent.
This commit is contained in:
Robert Speicher 2015-08-25 15:24:53 -07:00
parent 59180c4f5a
commit 2c3e42e4a4
11 changed files with 21 additions and 41 deletions

View file

@ -0,0 +1,9 @@
# Applies a syntax highlighting color scheme CSS class to any element with the
# `js-syntax-highlight` class
#
# ### Example Markup
#
# <div class="js-syntax-highlight"></div>
#
$(document).on 'ready page:load', ->
$('.js-syntax-highlight').addClass(gon.user_color_scheme)

View file

@ -190,11 +190,12 @@ class ApplicationController < ActionController::Base
end
def add_gon_variables
gon.api_version = API::API.version
gon.default_avatar_url = URI::join(Gitlab.config.gitlab.url, ActionController::Base.helpers.image_path('no_avatar.png')).to_s
gon.default_issues_tracker = Project.new.default_issue_tracker.to_param
gon.api_version = API::API.version
gon.relative_url_root = Gitlab.config.gitlab.relative_url_root
gon.default_avatar_url = URI::join(Gitlab.config.gitlab.url, ActionController::Base.helpers.image_path('no_avatar.png')).to_s
gon.max_file_size = current_application_settings.max_attachment_size;
gon.max_file_size = current_application_settings.max_attachment_size
gon.relative_url_root = Gitlab.config.gitlab.relative_url_root
gon.user_color_scheme = Gitlab::ColorSchemes.for_user(current_user).css_class
if current_user
gon.current_user_id = current_user.id

View file

@ -58,7 +58,7 @@ module GitlabMarkdownHelper
@options = options
# see https://github.com/vmg/redcarpet#darling-i-packed-you-a-couple-renderers-for-lunch
rend = Redcarpet::Render::GitlabHTML.new(self, user_color_scheme_class, options)
rend = Redcarpet::Render::GitlabHTML.new(self, options)
# see https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
@markdown = Redcarpet::Markdown.new(rend, MARKDOWN_OPTIONS)

View file

@ -33,10 +33,6 @@ module PreferencesHelper
Gitlab::Themes.by_id(current_user.try(:theme_id)).css_class
end
def user_color_scheme_class
Gitlab::ColorSchemes.by_id(current_user.try(:color_scheme_id)).css_class
end
def prefer_readme?
!current_user ||
current_user.project_view == 'readme'

View file

@ -7,4 +7,4 @@
%strong
= blob.filename
.file-content.code.term
= render 'shared/file_highlight', blob: blob, first_line_number: blob.startline, user_color_scheme_class: 'white'
= render 'shared/file_highlight', blob: blob, first_line_number: blob.startline

View file

@ -23,7 +23,7 @@
.nothing-here-block Empty file
- else
.file-content.code
%div.highlighted-data{class: user_color_scheme_class}
%div.highlighted-data.js-syntax-highlight
.line-numbers
- snippet_blob[:snippet_chunks].each do |snippet|
- unless snippet[:data].empty?

View file

@ -7,4 +7,4 @@
%strong
= wiki_blob.filename
.file-content.code.term
= render 'shared/file_highlight', blob: wiki_blob, first_line_number: wiki_blob.startline, user_color_scheme_class: 'white'
= render 'shared/file_highlight', blob: wiki_blob, first_line_number: wiki_blob.startline

View file

@ -1,4 +1,4 @@
.file-content.code{class: user_color_scheme_class}
.file-content.code.js-syntax-highlight
.line-numbers
- if blob.data.present?
- blob.data.lines.each_index do |index|

View file

@ -4,9 +4,8 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
attr_reader :template
alias_method :h, :template
def initialize(template, color_scheme, options = {})
def initialize(template, options = {})
@template = template
@color_scheme = color_scheme
@options = options.dup
@options.reverse_merge!(
@ -35,7 +34,7 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
end
formatter = Rouge::Formatters::HTMLGitlab.new(
cssclass: "code highlight #{@color_scheme} #{lexer.tag}"
cssclass: "code highlight js-syntax-highlight #{lexer.tag}"
)
formatter.format(lexer.lex(code))
end

View file

@ -224,8 +224,4 @@ describe 'GitLab Markdown', feature: true do
def current_user
@feat.user
end
def user_color_scheme_class
:white
end
end

View file

@ -48,25 +48,4 @@ describe PreferencesHelper do
]
end
end
describe 'user_color_scheme_class' do
context 'with current_user is nil' do
it 'should return a string' do
allow(self).to receive(:current_user).and_return(nil)
expect(user_color_scheme_class).to be_kind_of(String)
end
end
context 'with a current_user' do
(1..5).each do |color_scheme_id|
context "with color_scheme_id == #{color_scheme_id}" do
it 'should return a string' do
current_user = double(color_scheme_id: color_scheme_id)
allow(self).to receive(:current_user).and_return(current_user)
expect(user_color_scheme_class).to be_kind_of(String)
end
end
end
end
end
end