Skip to content

Commit

Permalink
Restrict Rouge formatters to Rouge::Formatters namespace
Browse files Browse the repository at this point in the history
ff0218a added support for specifying custom Rouge formatters with the
constraint that the formatter be in theRouge::Formatters namespace, but
it did not actually enforce this constraint. For example, this is valid:

```ruby
Rouge::Formatters.const_get('CSV')
=> CSV
```

Adding the `false` parameter to `const_get` prevents this:

```ruby
Rouge::Formatters.const_get('CSV', false)
NameError: uninitialized constant Rouge::Formatters::CSV
```
  • Loading branch information
stanhu committed Mar 14, 2021
1 parent 9cb5afc commit d6a1cbc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/kramdown/converter/syntax_highlighter/rouge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def self.formatter_class(opts = {})
when Class
formatter
when /\A[[:upper:]][[:alnum:]_]*\z/
::Rouge::Formatters.const_get(formatter)
::Rouge::Formatters.const_get(formatter, false)
else
# Available in Rouge 2.0 or later
::Rouge::Formatters::HTMLLegacy
Expand Down
18 changes: 11 additions & 7 deletions test/test_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
end

# custom formatter for tests
class RougeHTMLFormatters < Kramdown::Converter::SyntaxHighlighter::Rouge.formatter_class
module Rouge
module Formatters
class RougeHTMLFormatters < Kramdown::Converter::SyntaxHighlighter::Rouge.formatter_class

tag 'rouge_html_formatters'
tag 'rouge_html_formatters'

def stream(tokens, &b)
yield %(<div class="custom-class">)
super
yield %(</div>)
end
def stream(tokens, &b)
yield %(<div class="custom-class">)
super
yield %(</div>)
end

end
end
end
rescue LoadError, SyntaxError, NameError
end
Expand Down

0 comments on commit d6a1cbc

Please sign in to comment.