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 end
def add_gon_variables 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.default_issues_tracker = Project.new.default_issue_tracker.to_param
gon.api_version = API::API.version gon.max_file_size = current_application_settings.max_attachment_size
gon.relative_url_root = Gitlab.config.gitlab.relative_url_root 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.user_color_scheme = Gitlab::ColorSchemes.for_user(current_user).css_class
gon.max_file_size = current_application_settings.max_attachment_size;
if current_user if current_user
gon.current_user_id = current_user.id gon.current_user_id = current_user.id

View File

@ -58,7 +58,7 @@ module GitlabMarkdownHelper
@options = options @options = options
# see https://github.com/vmg/redcarpet#darling-i-packed-you-a-couple-renderers-for-lunch # 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 # see https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
@markdown = Redcarpet::Markdown.new(rend, MARKDOWN_OPTIONS) @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 Gitlab::Themes.by_id(current_user.try(:theme_id)).css_class
end end
def user_color_scheme_class
Gitlab::ColorSchemes.by_id(current_user.try(:color_scheme_id)).css_class
end
def prefer_readme? def prefer_readme?
!current_user || !current_user ||
current_user.project_view == 'readme' current_user.project_view == 'readme'

View File

@ -7,4 +7,4 @@
%strong %strong
= blob.filename = blob.filename
.file-content.code.term .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 .nothing-here-block Empty file
- else - else
.file-content.code .file-content.code
%div.highlighted-data{class: user_color_scheme_class} %div.highlighted-data.js-syntax-highlight
.line-numbers .line-numbers
- snippet_blob[:snippet_chunks].each do |snippet| - snippet_blob[:snippet_chunks].each do |snippet|
- unless snippet[:data].empty? - unless snippet[:data].empty?

View File

@ -7,4 +7,4 @@
%strong %strong
= wiki_blob.filename = wiki_blob.filename
.file-content.code.term .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 .line-numbers
- if blob.data.present? - if blob.data.present?
- blob.data.lines.each_index do |index| - blob.data.lines.each_index do |index|

View File

@ -4,9 +4,8 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
attr_reader :template attr_reader :template
alias_method :h, :template alias_method :h, :template
def initialize(template, color_scheme, options = {}) def initialize(template, options = {})
@template = template @template = template
@color_scheme = color_scheme
@options = options.dup @options = options.dup
@options.reverse_merge!( @options.reverse_merge!(
@ -35,7 +34,7 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
end end
formatter = Rouge::Formatters::HTMLGitlab.new( 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)) formatter.format(lexer.lex(code))
end end

View File

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

View File

@ -48,25 +48,4 @@ describe PreferencesHelper do
] ]
end end
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 end