Rouge gem not showing line breaks in formatted code?
Asked Answered
S

1

1

I have followed minimal examples using the rouge gem here (i.e. the documentation), as well as here and here.

Everything working, except that line breaks in the code aren't appearing.

To be explicit, code like this

def plus_two(x)
  x + 2
end

has the colours highlighted correctly, but is rendered across one line, like so

def plus_two(x) x + 2 end 

How can I make it render with the line breaks (just as in the code file)

Spue answered 22/8, 2019 at 14:48 Comment(0)
S
1

Change

require 'rouge'
source = "def plus_two(x)\n  x + 2\nend"
formatter = Rouge::Formatters::HTML.new
lexer = Rouge::Lexers::R.new
@code = formatter.format(lexer.lex(source)) 

enter image description here

to

require 'rouge'
source = "def plus_two(x)\n  x + 2\nend"
formatter = Rouge::Formatters::HTML.new
formatter = Rouge::Formatters::HTMLLinewise.new(formatter, class: 'line-%i')
lexer = Rouge::Lexers::R.new
@code = formatter.format(lexer.lex(source))  

enter image description here

More formatting options here

Spue answered 22/8, 2019 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.