Commit 4218a2bf authored by Stan Hu's avatar Stan Hu

Fix code preview theme setting for comments, issues, merge requests, and snippets.

Also preserve code preview color scheme in events dashboard.

Assign default colors to all code blocks shown as <pre class="code highlight [color_scheme]">

Closes #1139
parent 332413d7
...@@ -7,6 +7,7 @@ v 7.9.0 (unreleased) ...@@ -7,6 +7,7 @@ v 7.9.0 (unreleased)
- Add issue and merge request events to HipChat and Slack services (Stan Hu) - Add issue and merge request events to HipChat and Slack services (Stan Hu)
- Fix merge request URL passed to Webhooks. (Stan Hu) - Fix merge request URL passed to Webhooks. (Stan Hu)
- Fix bug that caused a server error when editing a comment to "+1" or "-1" (Stan Hu) - Fix bug that caused a server error when editing a comment to "+1" or "-1" (Stan Hu)
- Fix code preview theme setting for comments, issues, merge requests, and snippets (Stan Hu)
- Move labels/milestones tabs to sidebar - Move labels/milestones tabs to sidebar
- Upgrade Rails gem to version 4.1.9. - Upgrade Rails gem to version 4.1.9.
- Improve error messages for file edit failures - Improve error messages for file edit failures
......
/* https://github.com/MozMorris/tomorrow-pygments */ /* https://github.com/MozMorris/tomorrow-pygments */
pre.code.highlight.dark,
.code.dark { .code.dark {
background-color: #1d1f21;
color: #c5c8c6;
pre.code, pre.code,
.line-numbers, .line-numbers,
.line-numbers a { .line-numbers a {
......
/* https://github.com/richleland/pygments-css/blob/master/monokai.css */ /* https://github.com/richleland/pygments-css/blob/master/monokai.css */
pre.code.monokai,
.code.monokai { .code.monokai {
background: #272822;
color: #f8f8f2;
pre.highlight, pre.highlight,
.line-numbers, .line-numbers,
.line-numbers a { .line-numbers a {
......
/* https://gist.github.com/qguv/7936275 */ /* https://gist.github.com/qguv/7936275 */
pre.code.highlight.solarized-dark,
.code.solarized-dark { .code.solarized-dark {
background-color: #002b36;
color: #93a1a1;
pre.code, pre.code,
.line-numbers, .line-numbers,
.line-numbers a { .line-numbers a {
......
/* https://gist.github.com/qguv/7936275 */ /* https://gist.github.com/qguv/7936275 */
pre.code.highlight.solarized-light,
.code.solarized-light { .code.solarized-light {
background-color: #fdf6e3;
color: #586e75;
pre.code, pre.code,
.line-numbers, .line-numbers,
.line-numbers a { .line-numbers a {
......
/* https://github.com/aahan/pygments-github-style */ /* https://github.com/aahan/pygments-github-style */
pre.code.highlight.white,
.code.white { .code.white {
background-color: #fff;
color: #333;
pre.highlight, pre.highlight,
.line-numbers, .line-numbers,
.line-numbers a { .line-numbers a {
......
...@@ -166,7 +166,7 @@ module EventsHelper ...@@ -166,7 +166,7 @@ module EventsHelper
def event_note(text) def event_note(text)
text = first_line_in_markdown(text, 150) text = first_line_in_markdown(text, 150)
sanitize(text, tags: %w(a img b pre code p)) sanitize(text, tags: %w(a img b pre code p span))
end end
def event_commit_title(message) def event_commit_title(message)
......
...@@ -31,7 +31,9 @@ module GitlabMarkdownHelper ...@@ -31,7 +31,9 @@ module GitlabMarkdownHelper
def markdown(text, options={}) def markdown(text, options={})
unless (@markdown and options == @options) unless (@markdown and options == @options)
@options = options @options = options
gitlab_renderer = Redcarpet::Render::GitlabHTML.new(self, { gitlab_renderer = Redcarpet::Render::GitlabHTML.new(self,
user_color_scheme_class,
{
# 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-
filter_html: true, filter_html: true,
with_toc_data: true, with_toc_data: true,
......
...@@ -3,8 +3,9 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML ...@@ -3,8 +3,9 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
attr_reader :template attr_reader :template
alias_method :h, :template alias_method :h, :template
def initialize(template, options = {}) def initialize(template, color_scheme, options = {})
@template = template @template = template
@color_scheme = color_scheme
@project = @template.instance_variable_get("@project") @project = @template.instance_variable_get("@project")
@options = options.dup @options = options.dup
super options super options
...@@ -34,7 +35,7 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML ...@@ -34,7 +35,7 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
end end
formatter = Rugments::Formatters::HTML.new( formatter = Rugments::Formatters::HTML.new(
cssclass: "code highlight white #{lexer.tag}" cssclass: "code highlight #{@color_scheme} #{lexer.tag}"
) )
formatter.format(lexer.lex(code)) formatter.format(lexer.lex(code))
end end
......
...@@ -4,6 +4,8 @@ describe EventsHelper do ...@@ -4,6 +4,8 @@ describe EventsHelper do
include ApplicationHelper include ApplicationHelper
include GitlabMarkdownHelper include GitlabMarkdownHelper
let(:current_user) { create(:user, email: "current@email.com") }
it 'should display one line of plain text without alteration' do it 'should display one line of plain text without alteration' do
input = 'A short, plain note' input = 'A short, plain note'
expect(event_note(input)).to match(input) expect(event_note(input)).to match(input)
...@@ -50,4 +52,14 @@ describe EventsHelper do ...@@ -50,4 +52,14 @@ describe EventsHelper do
expect(event_note(input)).to match(link_url) expect(event_note(input)).to match(link_url)
expect(event_note(input)).to match(expected_link_text) expect(event_note(input)).to match(expected_link_text)
end end
it 'should preserve code color scheme' do
input = "```ruby\ndef test\n 'hello world'\nend\n```"
expected = '<pre class="code highlight white ruby">' \
"<code><span class=\"k\">def</span> <span class=\"nf\">test</span>\n" \
" <span class=\"s1\">\'hello world\'</span>\n" \
"<span class=\"k\">end</span>\n" \
'</code></pre>'
expect(event_note(input)).to eq(expected)
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment