Skip to content

Commit

Permalink
helpers/documentation: don't colorize twice
Browse files Browse the repository at this point in the history
Fixes #2181 (Bad formatting)

One of the regexps matches against string that were already colored. To prevent
that I used a negative look-ahead and rejected color sequences.
  • Loading branch information
kyrylo committed Apr 10, 2021
1 parent 033f69b commit 84fa00a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

### [v0.14.0][v0.14.0] (February 8, 2021)

#### Bug fixes

* Fixed bad formatting of some RDoc-style docs
(([#2182](https://github.com/pry/pry/pull/2182))

#### Features

* Made `?` an alias to `show-source -d`
Expand Down
3 changes: 2 additions & 1 deletion lib/pry/helpers/documentation_helpers.rb
Expand Up @@ -17,12 +17,13 @@ def process_rdoc(comment)
last_match_ruby = proc do
SyntaxHighlighter.highlight(Regexp.last_match(1))
end

comment.gsub(%r{<code>(?:\s*\n)?(.*?)\s*</code>}m, &last_match_ruby)
.gsub(%r{<em>(?:\s*\n)?(.*?)\s*</em>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
.gsub(%r{<i>(?:\s*\n)?(.*?)\s*</i>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
.gsub(%r{<tt>(?:\s*\n)?(.*?)\s*</tt>}m, &last_match_ruby)
.gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{Regexp.last_match(1)}\e[0m" }
.gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/, &last_match_ruby)
.gsub(/((?:^[ \t]+(?:(?!.+\e\[)).+(?:\n+|\Z))+)/, &last_match_ruby)
.gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{last_match_ruby.call}`" }
end

Expand Down
4 changes: 4 additions & 0 deletions spec/documentation_helper_spec.rb
Expand Up @@ -72,5 +72,9 @@
it "should not remove ++" do
expect(@helper.process_rdoc("--\n comment in a bubble\n++")).to match(/\+\+/)
end

it "should not syntax highlight already highlighted code" do
expect(@helper.process_rdoc(" \e\[31mFOO\e\[0m")).to match(/ \e\[31mFOO\e\[0m/)
end
end
end

0 comments on commit 84fa00a

Please sign in to comment.