From 5348ee62787a12855d8d917580fdcc8900910386 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 7 Sep 2012 22:14:17 +0200 Subject: [PATCH 1/4] Fix Ruby anti-pattern in Markdown --- lib/gitlab/markdown.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 17f865bb..afb61364 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -80,7 +80,7 @@ module Gitlab # # Returns parsed text def parse(text) - text = text.gsub(REFERENCE_PATTERN) do |match| + text.gsub!(REFERENCE_PATTERN) do |match| prefix = $1 || '' reference = $2 identifier = $3 || $4 || $5 @@ -93,7 +93,7 @@ module Gitlab end end - text = text.gsub(EMOJI_PATTERN) do |match| + text.gsub!(EMOJI_PATTERN) do |match| if valid_emoji?($2) image_tag("emoji/#{$2}.png", size: "20x20", class: 'emoji', title: $1, alt: $1) else From 9a4c22d395b96e27f9a1c5ec90b161ddf5815cdd Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sat, 8 Sep 2012 01:11:32 +0200 Subject: [PATCH 2/4] Remove unnecessary check in Markdown helper --- app/helpers/gitlab_markdown_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb index ca2cb01f..e97e46f5 100644 --- a/app/helpers/gitlab_markdown_helper.rb +++ b/app/helpers/gitlab_markdown_helper.rb @@ -27,7 +27,7 @@ module GitlabMarkdownHelper filter_html: true, with_toc_data: true, hard_wrap: true) - @markdown ||= Redcarpet::Markdown.new(gitlab_renderer, + @markdown = Redcarpet::Markdown.new(gitlab_renderer, # see https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use no_intra_emphasis: true, tables: true, From ce3fb949398786c9cc9c9d7803b72cb57f661279 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sat, 8 Sep 2012 00:49:17 +0200 Subject: [PATCH 3/4] Fix bug where parsing of emoji was unnecessarily dependent on @project being set --- lib/gitlab/markdown.rb | 7 +++++-- spec/helpers/gitlab_markdown_helper_spec.rb | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index afb61364..f9125e03 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -47,7 +47,6 @@ module Gitlab # Note: reference links will only be generated if @project is set def gfm(text, html_options = {}) return text if text.nil? - return text if @project.nil? @html_options = html_options @@ -78,8 +77,11 @@ module Gitlab # # text - Text to parse # + # Note: reference links will only be generated if @project is set + # # Returns parsed text def parse(text) + # parse reference links text.gsub!(REFERENCE_PATTERN) do |match| prefix = $1 || '' reference = $2 @@ -91,8 +93,9 @@ module Gitlab else match end - end + end if @project + # parse emoji text.gsub!(EMOJI_PATTERN) do |match| if valid_emoji?($2) image_tag("emoji/#{$2}.png", size: "20x20", class: 'emoji', title: $1, alt: $1) diff --git a/spec/helpers/gitlab_markdown_helper_spec.rb b/spec/helpers/gitlab_markdown_helper_spec.rb index 3e7a02c6..4dd3802a 100644 --- a/spec/helpers/gitlab_markdown_helper_spec.rb +++ b/spec/helpers/gitlab_markdown_helper_spec.rb @@ -247,6 +247,11 @@ describe GitlabMarkdownHelper do it "ignores invalid emoji" do gfm(":invalid-emoji:").should_not match(/ Date: Sat, 8 Sep 2012 01:58:12 +0200 Subject: [PATCH 4/4] Prevent gfm() to leak changes to the string supplied through the _text_ argument --- lib/gitlab/markdown.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index f9125e03..4fc0c392 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -48,6 +48,9 @@ module Gitlab def gfm(text, html_options = {}) return text if text.nil? + # prevents the string supplied through the _text_ argument to be altered + text = text.dup + @html_options = html_options # Extract pre blocks so they are not altered