Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Rouge 1.x and 2.0 #413

Closed
wants to merge 1 commit into from

Conversation

5t111111
Copy link
Contributor

@5t111111 5t111111 commented Feb 22, 2017

This PR is kind of a draft proposal for supporting Rouge 2.0.

Since for the moment Kramdown supports Rouge < 2.0 which is considerably outdated, little by little it begins to hard to coexist with other gems which support Rouge >= 2.0 only.

I am still not sure what is the best way to support Rouge 2.0. Maybe we can support Rouge >= 2.0 only like other gems do (e.g. middleman-syntax drops Rouge 1.x support). However, I doubt that it is comfortable way for Kramdown because Rouge is an optional dependency and seem not to be easy to fix version to 2.0 or later.

So, this is the PR for supporting both Rouge 1.x and 2.0. Syntax highlighting will work just the same as before with Rouge 2.0, but it spoils some goodness, testability, for example.

How does this sound to you?

Related to #350

@gettalong gettalong self-assigned this Feb 26, 2017
rescue NameError
# Fallback to Rouge 1
formatter_class = ::Rouge::Formatters::HTML
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This begin ... rescue ... end block should go outside the method and inside the begin ... rescue block where we test for availability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if I properly understand what you mean.

Do you mean that begin ... rescue ... end should be placed like these ones like the below snippet?

...

  module Rouge

...

    begin
      FORMATTER_CLASS = ::Rouge::Formatters::HTMLLegacy
    rescue NameError
      FORMATTER_CLASS = ::Rouge::Formatters::HTML
    end

    def self.call(converter, text, lang, type, call_opts)

      ...

      formatter = (opts.fetch(:formatter, FORMATTER_CLASS)).new(opts)
      formatter.format(lexer.lex(text))
    end

...

  end

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but directly into the other begin ... rescue block, right after require 'rouge'. This avoids doing this over and over again for each code block.

begin
# Rouge::Formatters::HTMLLegacy is available on Rouge 2.0 or later
formatter_class = ::Rouge::Formatters::HTMLLegacy
opts[:css_class] ||= 'highlight'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you assigning the 'highlight' CSS class here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is requirement for backward compatibility.

While Rouge < 2.0's used to add 'highlight' class to parsed codes, Rouge >= 2.0 adds 'codehilite' by default even if you use Rouge::Formatters::HTMLLegacy. So, without specifing an explicit css class, users perhaps encounter regression.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

@5t111111
Copy link
Contributor Author

@gettalong Thanks for letting me know about your suggestion. Fixed!

end

def self.call(converter, text, lang, type, call_opts)
opts = options(converter, type)
call_opts[:default_lang] = opts[:default_lang]
lexer = ::Rouge::Lexer.find_fancy(lang || opts[:default_lang], text)
return nil if opts[:disable] || !lexer

formatter = (opts.fetch(:formatter, ::Rouge::Formatters::HTML)).new(opts)
opts[:css_class] ||= 'highlight' # For backward compatibility when using Rouge 2.0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Althogh this line actually does almost nothing when using Rouge < 2.0, I think it is better than checking a formatter's class in the method.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

rescue LoadError, SyntaxError
AVAILABLE = false # :nodoc:
rescue NameError
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was putting the whole begin ... rescue ... end block inside the begin...rescue block where availability is checked. Then it is clear which exception handling mechanism handles which situation.

@gettalong
Copy link
Owner

@5t111111 Some more things:

  • Please update the rouge documentation at doc/syntax_highlighter/rouge.page
  • Please update .travis.yml to make Travis test against rouge 1.x and 2.x (only if you know how to do this; otherwise I will do this later)
  • Please squash your commits into one

Thanks!

@5t111111
Copy link
Contributor Author

5t111111 commented Feb 28, 2017

  • Moved new rouge 2.0 formatter availability check block into the block where rogue gem availability is checked
  • Update the rouge documentation at doc/syntax_highlighter/rouge.page
  • Squash commits into one

Please update .travis.yml to make Travis test against rouge 1.x and 2.x (only if you know how to do this; otherwise I will do this later)

Can you please do it later? I am not sure what is the appropriate way to archive this purpose.

Please point out if you have any further suggestinons.

@gettalong
Copy link
Owner

Yes, I will do the Travis change later. Everything else looks great - thank you!

I will probably have some time this weekend to merge this in.

@Mousius
Copy link

Mousius commented Mar 10, 2017

@gettalong any idea when this will get merged? 😄

@gettalong
Copy link
Owner

@DaMouse404 It will definitely be in the next release but my time for open-source work is currently very limited. So, maybe sometime this month.

@gettalong
Copy link
Owner

@5t111111 Thanks for you pull request - I have merged it and fixed the test runner and Travis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants