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
Closed

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

This comment has been minimized.

@gettalong

gettalong Feb 26, 2017
Owner

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

This comment has been minimized.

@5t111111

5t111111 Feb 26, 2017
Author Contributor

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

This comment has been minimized.

@gettalong

gettalong Feb 26, 2017
Owner

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'

This comment has been minimized.

@gettalong

gettalong Feb 26, 2017
Owner

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

This comment has been minimized.

@5t111111

5t111111 Feb 26, 2017
Author Contributor

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.

This comment has been minimized.

@gettalong

gettalong Feb 26, 2017
Owner

Thanks for the explanation!

@5t111111
Copy link
Contributor Author

@5t111111 5t111111 commented Feb 27, 2017

@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

This comment has been minimized.

@5t111111

5t111111 Feb 27, 2017
Author Contributor

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.

This comment has been minimized.

rescue LoadError, SyntaxError
AVAILABLE = false # :nodoc:
rescue NameError

This comment has been minimized.

@gettalong

gettalong Feb 27, 2017
Owner

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

@gettalong gettalong commented Feb 27, 2017

@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 5t111111 force-pushed the 5t111111:rouge-2-support branch from 036ffbc to cbb6266 Feb 28, 2017
@5t111111
Copy link
Contributor Author

@5t111111 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

@gettalong gettalong commented Feb 28, 2017

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.

@DaMouse404
Copy link

@DaMouse404 DaMouse404 commented Mar 10, 2017

@gettalong any idea when this will get merged? 😄

@gettalong
Copy link
Owner

@gettalong gettalong commented Mar 11, 2017

@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

@gettalong gettalong commented Mar 20, 2017

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

@gettalong gettalong closed this Mar 20, 2017
@5t111111 5t111111 deleted the 5t111111:rouge-2-support branch Mar 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.